This commit is contained in:
Louis Mazin 2026-09-21 14:44:20 +02:00
parent 8f324d99f5
commit a84edb734a

View File

@ -1,6 +1,7 @@
import sys
import logging
from django.apps import AppConfig
from django.core.management import execute_from_command_line
logger = logging.getLogger(__name__)
@ -15,6 +16,14 @@ class UsersConfig(AppConfig):
# Only run when the server is actually starting
if 'runserver' in sys.argv:
# Defer the superuser creation to avoid database access during app initialization
# This prevents the Django warning about accessing database during app initialization
import threading
import time
def create_superuser_if_needed():
# Wait a bit to ensure Django is fully ready
time.sleep(0.1)
try:
from .models import User
from decouple import config
@ -64,3 +73,8 @@ class UsersConfig(AppConfig):
except Exception as e:
# Don't fail the entire Django startup if there's an error
logger.error(f'Error creating admin user: {e}')
# Start the superuser creation in a separate thread to avoid the warning
thread = threading.Thread(target=create_superuser_if_needed, daemon=True)
thread.start()