2017-10-17 92 views
0

我想將第一個模型添加到已運行的應用程序,現在我無法啓動它,因爲它總是給出AppRegistryNotReady。這隻會發生,如果我的模型MailLogmodels.Model的孩子。Django AppRegistryNotReady定義模型時

from __future__ import unicode_literals 
from django.db import models 

#class MailLog(models.Model): # like this, it crashes 
class MailLog(): # like this, it works 
    # Field definitions 
    recipient = models.EmailField() 
    created = models.DateTimeField(auto_now_add=True) 
    template = models.CharField(max_length=500) 
    error = models.TextField(null=True) 

    def __str__(self): 
     return self.recipient+" "+self.template+"("+str(self.created)+")" 

錯誤發生時,不管有什麼類裏面,即使是隻有一個有pass。但是,我可以在admin.py中導入我的模型,並在從我稱爲core.py的文件導入時崩潰。這個文件看起來是這樣的:

import boto3 
from botocore.exceptions import ClientError 
from django.template import loader 
from django.conf import settings 
from .templates import * 
from .models import MailLog 

回溯看起來像這樣

Traceback (most recent call last): 
    File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line 
    utility.execute() 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 316, in execute 
    settings.INSTALLED_APPS 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__ 
    self._setup(name) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup 
    self._wrapped = Settings(settings_module) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/conf/__init__.py", line 97, in __init__ 
    mod = importlib.import_module(self.SETTINGS_MODULE) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/home/niels/Dokumente/jobapp/deploy/backend/settings.py", line 255, in <module> 
    django.setup() 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/__init__.py", line 27, in setup 
    apps.populate(settings.INSTALLED_APPS) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate 
    app_config = AppConfig.create(entry) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/config.py", line 90, in create 
    module = import_module(entry) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/home/niels/Dokumente/jobapp/deploy/ses_mail/__init__.py", line 1, in <module> 
    from .core import send 
    File "/home/niels/Dokumente/jobapp/deploy/ses_mail/core.py", line 7, in <module> 
    from .admin import MailLog 
    File "/home/niels/Dokumente/jobapp/deploy/ses_mail/admin.py", line 2, in <module> 
    from .models import * 
    File "/home/niels/Dokumente/jobapp/deploy/ses_mail/models.py", line 6, in <module> 
    class MailLog(models.Model): 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__ 
    app_config = apps.get_containing_app_config(module) 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config 
    self.check_apps_ready() 
    File "/home/niels/anaconda3/envs/app/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready 
    raise AppRegistryNotReady("Apps aren't loaded yet.") 
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 
+0

顯示模型的完整代碼以及你得到的完整回溯 –

+0

順便說一句,traceback中的from .admin import MailLog'看起來很奇怪 - 你應該直接從'.models'導入。 – Alasdair

回答

1

你不應該導入Django模型在應用程序的__init__(或使他們由core進口導入如果您使用一個空的__init__.py它應該停止錯誤

+0

非常感謝! – nielsrolf