django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

梦想与她 提交于 2019-12-12 03:18:55

问题


In Django 1.7 this code caused errors in django.setup() :

class MyModel(models.Model):
    special_foo=Foo.objects.filter(name__contains='special')

In my case there I got this:

django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

But I have seen recursion errors in django.setup() trying to run django.setup() again, too.


回答1:


I solved this with properties at class level.

class MyModel(models.Model):

    @classproperty
    def special_foo(cls):
        return Foo.objects.filter(name__contains='special')

Unfortunately python does not support @classproperty right out of the box yet.

I used the implementation from here https://stackoverflow.com/a/5191224/633961



来源:https://stackoverflow.com/questions/32095653/django-core-exceptions-appregistrynotready-models-arent-loaded-yet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!