问题
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