You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 both home and meetings are listed in your INSTALLED_APPS setting. 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 of meetings/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 Notification in 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 core or utils) to eliminate the circular dependency.

Quick Checks to Rule Out Simple Mistakes

  • Make sure Notification is actually defined in home/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:32:44