Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#25864 closed Uncategorized (needsinfo)

AppRegistryNotReady exception since update from 1.8.7 to 1.9

Reported by: stephanm Owned by: nobody
Component: Uncategorized Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi,

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

from myapp.models import MyModel1 # here is the exception
...
if __name__ == "__main__":
    
    import django
    django.setup()
    # my code follows

Change History (6)

comment:1 by stephanm, 8 years ago

comment:2 by Baptiste Mispelon, 8 years ago

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.

comment:3 by stephanm, 8 years ago

I tried but I didn't get it :-(

Actually I use the following workaround:

# 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.

comment:4 by Tim Graham, 8 years ago

Resolution: needsinfo
Status: newclosed

comment:5 by Aymeric Augustin, 8 years ago

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.

comment:6 by Aymeric Augustin, 8 years ago

I filed #27176 to improve the behavior in this area, but I'm not sure that's the problem you were hitting.

Note: See TracTickets for help on using tickets.
Back to Top