Django 2.0应用启动报错及模型导入失败问题咨询
Fixing Django 2.0 Errors: AppRegistryNotReady & Circular Import Issue
Hey there, let's break down these two frustrating Django 2.0 bugs you're hitting—they're super common, and we can sort them out quickly!
1. Fixing django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
This error pops up when your code tries to access Django models before Django has finished initializing its app registry. Here's how to fix it:
- Move model-dependent code out of module scope: If you have code that queries or instantiates models directly at the top of a file (like
models.py,views.py, or a custom script), move that logic inside a function or method. Django loads modules first, then initializes models—so top-level code runs too early. - Initialize Django in custom scripts: If you're running a standalone script alongside your Django app, you need to explicitly set up the Django environment first. Add this at the very start of your script:
import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings") django.setup() - Double-check your
INSTALLED_APPS: Make sure bothhomeandmeetingsare listed in yourINSTALLED_APPSsetting. Your current list cuts off, but it should look something like this:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Add your apps here 'home', 'meetings', ]
2. Fixing ImportError: cannot import name 'Notification'
This is almost certainly a circular import problem—meaning meetings/models.py is importing Notification from home/models.py, and somewhere in home/models.py, it's also importing something from meetings/models.py. Django gets stuck trying to load both modules at the same time. Here are your fixes:
- Use lazy imports: Move the import statement inside the function/method where you actually use
Notification, instead of keeping it at the top ofmeetings/models.py:# Bad: Top-level import (causes circular issue) # from home.models import Notification def send_meeting_alert(): # Good: Import only when needed from home.models import Notification notification = Notification.objects.create(...) - Use string references for model relationships: If you're using
Notificationin a model field (like a ForeignKey), replace the direct import with a string path to the model:# Instead of this: # from home.models import Notification # notification = models.ForeignKey(Notification, on_delete=models.CASCADE) # Do this: notification = models.ForeignKey('home.Notification', on_delete=models.CASCADE) - Refactor shared code: If both apps need to reference each other's models, consider moving shared logic or common models into a separate app (like
coreorutils) to eliminate the circular dependency.
Quick Checks to Rule Out Simple Mistakes
- Make sure
Notificationis actually defined inhome/models.py—no typos, and the class definition comes before any code that imports it. - Confirm neither app is missing from
INSTALLED_APPS(we covered this earlier, but it's worth double-checking!).
内容的提问来源于stack exchange,提问作者Nathan Fon
相关产品推荐
相关产品推荐

