24 lines
787 B
Python
24 lines
787 B
Python
from django.apps import AppConfig
|
|
import os
|
|
import sys
|
|
|
|
class ElasticConfig(AppConfig):
|
|
default_auto_field = "django.db.models.BigAutoField"
|
|
name = "elastic"
|
|
|
|
def ready(self):
|
|
# 避免在 migrate、collectstatic 等管理命令中执行
|
|
if os.environ.get('RUN_MAIN') != 'true':
|
|
# Django 开发服务器会启动两个进程,只在主进程执行
|
|
return
|
|
|
|
# 避免在 manage.py 命令(除 runserver 外)中执行
|
|
if 'runserver' not in sys.argv:
|
|
return
|
|
|
|
# 延迟导入,避免循环导入或过早加载
|
|
from .es_connect import create_index_with_mapping
|
|
try:
|
|
create_index_with_mapping()
|
|
except Exception as e:
|
|
print(f"❌ ES 初始化失败: {e}") |