I have a script which worked in django 1.8
but does no more in django 1.9, I get the exception:
{{{django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet}}}
Unfortunately I didn't find a hint in the release notes
{{{#!py
from myapp.models import MyModel1 # here is the exception
...
if __name__ == "__main__":
import django
django.setup()
# my code follows
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25864>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/25864#comment:1>
Comment (by bmispelon):
Hi,
We can't really reproduce your issue without the definition of your
models.
Is there a way you could provide them (ideally, a simplified version that
still reproduces the issue).
Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/25864#comment:2>
Comment (by stephanm):
I tried but I didn't get it :-(
Actually I use the following workaround:
{{{#!py
# at the beginning of the file before the first import:
if __name__ == "__main__":
import django
django.setup()
# now the imports ...
from myapp.models import MyModel1 # here is the exception
...
if __name__ == "__main__":
# skipped here ...
# import django
# django.setup()
# my code follows
}}}
I can live with this so you can close the ticket it if you like,
if I find a way to reproduce it in a simple "example" I will open it
again.
--
Ticket URL: <https://code.djangoproject.com/ticket/25864#comment:3>
* status: new => closed
* resolution: => needsinfo
--
Ticket URL: <https://code.djangoproject.com/ticket/25864#comment:4>
Comment (by aaugustin):
This is the expected behavior. You need to setup Django before importing
models.
To avoid the extra `if __name__ == '__main__'` I think we should make
`django.setup()` idempotent.
I'll write to the DevelopersMailingList about this idea.
--
Ticket URL: <https://code.djangoproject.com/ticket/25864#comment:5>
Comment (by aaugustin):
I filed #27176 to improve the behavior in this area, but I'm not sure
that's the problem you were hitting.
--
Ticket URL: <https://code.djangoproject.com/ticket/25864#comment:6>