为什么我得到django.core.exceptions.AppRegistryNotReady:模型还没有加载?

时间:2016-09-09 13:00:31

标签: django

由于我在每个app模型中都有相同的抽象模型,因此我必须避免冗余 我只是将我的常用抽象模型移动到我创建的新应用程序中,并称之为通用

Common Models.py现在是

from django.db import models

LANGUAGE_CHOICES = (
    ('E','ENGLISH'),
    ('F','FRENCH'),
    ('S','SPANISH'),
)




class CommonInfo(models.Model):
    created = models.DateTimeField("creation date", auto_now_add=True)
    modified = models.DateTimeField("modification date", auto_now=True)
    description = models.TextField()

    class Meta:
        abstract = True 

然后我将我的常用应用程序添加到settings.py

最后,在我添加的所有应用中

from common.models import CommonInfo
from common.models import LANGUAGE_CHOICES

运行本地服务器后,这是我在终端中获得的错误(完整跟踪)

Traceback (most recent call last):   File "manage.py", line 10, in
<module>
execute_from_command_line(sys.argv)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\core\m
anagement\__init__.py", line 338, in execute_from_command_line
utility.execute()   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\core\m
anagement\__init__.py", line 312, in execute
django.setup()   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\__init
__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\apps\r
egistry.py", line 108, in populate
app_config.import_models(all_models)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\apps\c
onfig.py", line 198, in import_models
self.models_module = import_module(models_module_name)   File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)   File "C:\Users\Boris\dev\rentout\rentout\lease\models.py", line 94, in
<modu le>
class LeaseFilter(django_filters.FilterSet):   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filter
s\filterset.py", line 181, in __new__
filters = new_class.filters_for_model(opts.model, opts)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filter
s\filterset.py", line 456, in filters_for_model
cls.filter_for_reverse_field   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filter
s\filterset.py", line 104, in filters_for_model
filter_ = filter_for_field(field, f)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filter
s\filterset.py", line 461, in filter_for_field
f, lookup_type = resolve_field(f, lookup_expr)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django_filter
s\utils.py", line 104, in resolve_field
query = model_field.model._default_manager.all().query   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\manager.py", line 228, in all
return self.get_queryset()   File "C:\Users\Boris\dev\rentout\rentout\lease\models.py", line 15, in
get_q ueryset
return super(NotTerminatedActiveManager, self).get_queryset().filter(is_term inated=False,is_active=True)
File
"C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\query.py", line 697, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\sql\query.py", line 1301, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)   File
"C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\sql\query.py", line 1328, in _add_q
current_negated=current_negated, connector=connector, allow_joins=allow_join s)   File
"C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\sql\query.py", line 1144, in build_filter
lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)   File
"C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\sql\query.py", line 1030, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_met a())   File
"C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\sql\query.py", line 1383, in names_to_path
field_names = list(get_field_names_from_opts(opts))   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\sql\query.py", line 43, in get_field_names_from_opts
for f in opts.get_fields()   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\options.py", line 740, in get_fields
return self._get_fields(include_parents=include_parents, include_hidden=incl ude_hidden)   File
"C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\options.py", line 802, in _get_fields
all_fields = self._relation_tree   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\utils\
functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\options.py", line 709, in _relation_tree
return self._populate_directed_relation_graph()   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\db\mod
els\options.py", line 681, in _populate_directed_relation_graph
all_models = self.apps.get_models(include_auto_created=True)   File
"C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\utils\
lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\apps\r
egistry.py", line 168, in get_models
self.check_models_ready()   File "C:\Users\Boris\dev\rentout\virtrentout\lib\site-packages\django\apps\r
egistry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

可能是什么问题?如果有一种更好的方法可以将所有常见的抽象模型和选择分组以避免延迟?

更新:

这是我忘记包含的错误的位置

   from django.db import models
from concurrency.fields import IntegerVersionField
from client.models import Tenant
from unit.models import Unit, Extra
from common.models import CommonInfo
from common.models import  NOTE_STATUS_CHOICES
import django_filters

# Create your models here.



class NotTerminatedActiveManager(models.Manager):
    def get_queryset(self):
        return super(NotTerminatedActiveManager, self).get_queryset().filter(is_terminated=False,is_active=True)


class Lease(CommonInfo):
    version = IntegerVersionField( )
    amount  = models.DecimalField(max_digits=7, decimal_places=2)
    is_notrenewed = models.BooleanField(default=False)
    unit = models.ForeignKey(Unit)
    is_terminated = models.BooleanField(default=False)
    not_terminated_active_objects = NotTerminatedActiveManager() 
    def __unicode__(self):
        return u'%s %i %s %s ' % ("#", self.id,"unit", self.unit)

class LeaseConditions(CommonInfo):
    version = IntegerVersionField( )
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    lease = models.ForeignKey(Lease)
    increase  = models.DecimalField(max_digits=7, decimal_places=2)
    amount  = models.DecimalField(max_digits=7, decimal_places=2)
    is_terminated = models.BooleanField(default=False)
    not_terminated_active_objects = NotTerminatedActiveManager() 
    def __unicode__(self):
        return u'%s %i %s %s %s %s %s %s' % ("#", self.id,"first_name", self.first_name, "last_name",  self.last_name, "phone", self.phone )

    def clean(self):
        model = self.__class__
        if self.lease_id and (self.is_terminated == False) and model.objects.filter(lease=self.lease, is_active=True ).count() == 1:
            raise ValidationError('!Lease has a active condition already, Terminate prior to creation of new one'.format(self.lease))

class LeaseTenant(CommonInfo):
    version = IntegerVersionField( )
    tenant = models.ForeignKey(Tenant)
    lease = models.ForeignKey(Lease)
    is_financialy_accountable = models.BooleanField(default=True)


class LeaseExtra(CommonInfo):
    version = IntegerVersionField( )
    extra = models.ForeignKey(Extra)
    lease = models.ForeignKey(Lease)
    is_included = models.BooleanField(default=True)





class LeaseDiscount(CommonInfo):
    version = IntegerVersionField( )
    amount  = models.DecimalField(max_digits=7, decimal_places=2)
    leaseconditions = models.ForeignKey(LeaseConditions)
    period_date = models.DateTimeField()


class LeasePayment(CommonInfo):
    version = IntegerVersionField( )
    amount  = models.DecimalField(max_digits=7, decimal_places=2)
    lease = models.ForeignKey(Lease)
    period_payed_for = models.DateTimeField()
    payment_date = models.DateTimeField()



class Note(CommonInfo):
    version = IntegerVersionField( )
    title = models.CharField(max_length=200)
    contact = models.ForeignKey(Lease)
    followup_date = models.DateTimeField(null=True, blank=True)
    status = models.CharField(max_length=1, default='P',
                              choices=NOTE_STATUS_CHOICES)
    #widgets = {
            #Use localization and bootstrap 3
            #'datetime': DateTimeWidget(attrs={'id':"id_followup_date"}, usel10n = True, bootstrap_version=3)}
    def __unicode__(self):
        return u'%s %s %s %s %s %i %s %s %s %s %s %s %s %s' % ("Status:",  self.status, "Follow Date:", self.followup_date, "Note #:", self.id, "Published:", self.creation_time, "Author:",self.author,"/Title:" , self.title,"/Body:" , self.body  )




class LeaseFilter(django_filters.FilterSet):
    class Meta:
        model = Lease
        fields = ['is_notrenewed', 'unit',
        'is_terminated']
        #provider = django_filters.ModelChoiceFilter(queryset=Provider.objects.all(), 
        #name = django_filters.ModelChoiceFilter(queryset=Lease.objects.all().order_by('name'))

1 个答案:

答案 0 :(得分:1)

我不认为LeaseFilter类属于您的模型。它看起来好像是在加载类时尝试进行查询。由于尚未加载模型,因此会导致错误。尝试将课程移到其他地方,例如views.py