Files
Achievement_Inputing/elastic/apps.py
2025-11-14 21:15:02 +08:00

26 lines
894 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, get_type_list
try:
create_index_with_mapping()
types = get_type_list()
print(f"🔎 启动时 type_list: {types}")
except Exception as e:
print(f"❌ ES 初始化失败: {e}")