0

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.

Tilo
  • 3,179
  • 25
  • 29
guettli
  • 23,964
  • 63
  • 293
  • 556

1 Answers1

0

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

Community
  • 1
  • 1
guettli
  • 23,964
  • 63
  • 293
  • 556