[Django] #26023: Importing GenericForeignKey on Django 1.9

73 views
Skip to first unread message

Django

unread,
Jan 2, 2016, 10:19:46 AM1/2/16
to django-...@googlegroups.com
#26023: Importing GenericForeignKey on Django 1.9
--------------------------------------+----------------------------------
Reporter: Chris7 | Owner: nobody
Type: Bug | Status: new
Component: contrib.contenttypes | Version: 1.9
Severity: Normal | Keywords: generic foreign keys
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+----------------------------------
It appears it is not possible to import GenericForeignKey from outside a
live Django project in 1.9. To reproduce this error:


{{{
from django.conf import settings
settings.configure()
from django.contrib.contenttypes.fields import GenericForeignKey
}}}

Produces:

{{{
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/contrib/contenttypes/fields.py", line 5, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/contrib/contenttypes/models.py", line 159, in <module>
class ContentType(models.Model):
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
}}}

My guess is contenttypes is attempting to build the list of models, but
failing because there is no app to initialize.

--
Ticket URL: <https://code.djangoproject.com/ticket/26023>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 2, 2016, 10:25:33 AM1/2/16
to django-...@googlegroups.com
#26023: Importing GenericForeignKey on Django 1.9
-------------------------------------+-------------------------------------

Reporter: Chris7 | Owner: nobody
Type: Bug | Status: new
Component: | Version: 1.9
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: generic foreign | Triage Stage:
keys | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Could you clarify the use case? You need to have
`django.contrib.contentypes` in `INSTALLED_APPS`, I believe.

--
Ticket URL: <https://code.djangoproject.com/ticket/26023#comment:1>

Django

unread,
Jan 2, 2016, 10:25:57 AM1/2/16
to django-...@googlegroups.com
#26023: Importing GenericForeignKey on Django 1.9
-------------------------------------+-------------------------------------

Reporter: Chris7 | Owner: nobody
Type: Bug | Status: new
Component: | Version: 1.9
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: generic foreign | Triage Stage:
keys | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Chris7:

Old description:

New description:

It appears it is not possible to import GenericForeignKey from outside a
live Django project in 1.9. To reproduce this error:


{{{
from django.conf import settings
settings.configure()
from django.contrib.contenttypes.fields import GenericForeignKey
}}}

Produces:

{{{
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/contrib/contenttypes/fields.py", line 5, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/contrib/contenttypes/models.py", line 159, in <module>
class ContentType(models.Model):
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
}}}

This seems to be occurring because ModelBase is attempting to get the app
config, but the configuration step has not occurred.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26023#comment:2>

Django

unread,
Jan 2, 2016, 10:31:00 AM1/2/16
to django-...@googlegroups.com
#26023: Importing GenericForeignKey on Django 1.9
-------------------------------------+-------------------------------------

Reporter: Chris7 | Owner: nobody
Type: Bug | Status: new
Component: | Version: 1.9
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: generic foreign | Triage Stage:
keys | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Chris7):

The case is for the app [https://github.com/wooey/Wooey/ Wooey] and this
[https://github.com/wooey/Wooey/issues/101 issue].

We have an option that bootstraps the installation process which entails
setting up Django project with pre-determined settings. For compatibility
with older versions of Django, there is a compatibility shim to handle
cases like GFK's import location being changed and the template engine's
get_string method. So at the onset, there is no INSTALLED_APPS. It's a
python script importing GFK. I could move these imports to another shim
(or do some other workarounds) but this approach works in other Django
versions -- although it could entirely be fortunate this behavior was not
flagged.

--
Ticket URL: <https://code.djangoproject.com/ticket/26023#comment:3>

Django

unread,
Jan 2, 2016, 11:00:56 AM1/2/16
to django-...@googlegroups.com
#26023: Importing GenericForeignKey on Django 1.9
-------------------------------------+-------------------------------------

Reporter: Chris7 | Owner: nobody
Type: Bug | Status: new
Component: | Version: 1.9
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: generic foreign | Triage Stage:
keys | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

I'm not sure about fixing the issue in general, but in that case I'd
suggest dropping compatibility with Django < 1.8 since those versions are
unsupported.

--
Ticket URL: <https://code.djangoproject.com/ticket/26023#comment:4>

Django

unread,
Jan 2, 2016, 3:04:19 PM1/2/16
to django-...@googlegroups.com
#26023: Importing GenericForeignKey on Django 1.9
-------------------------------------+-------------------------------------
Reporter: Chris7 | Owner: nobody
Type: Bug | Status: closed
Component: | Version: 1.9
contrib.contenttypes |
Severity: Normal | Resolution: invalid

Keywords: generic foreign | Triage Stage:
keys | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => closed
* resolution: => invalid


Comment:

You must call `django.setup()` as highlighted in the 1.7 release notes.

https://docs.djangoproject.com/en/1.7/releases/1.7/#standalone-scripts

--
Ticket URL: <https://code.djangoproject.com/ticket/26023#comment:5>

Django

unread,
Jan 6, 2016, 2:35:36 PM1/6/16
to django-...@googlegroups.com
#26023: Importing GenericForeignKey on Django 1.9
-------------------------------------+-------------------------------------
Reporter: Chris7 | Owner: nobody

Type: Bug | Status: closed
Component: | Version: 1.9
contrib.contenttypes |
Severity: Normal | Resolution: invalid
Keywords: generic foreign | Triage Stage:
keys | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Chris7):

An additional error arises for that case:


{{{
>>> from django.conf import settings
>>> settings.configure()

>>> import django
>>> django.setup()
>>> from django.contrib.contenttypes.fields import GenericForeignKey


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/contrib/contenttypes/fields.py", line 5, in <module>
from django.contrib.contenttypes.models import ContentType
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-
packages/django/contrib/contenttypes/models.py", line 159, in <module>
class ContentType(models.Model):
File "/home/chris/.virtualenvs/wooey/local/lib/python2.7/site-

packages/django/db/models/base.py", line 103, in __new__
"application was loaded. " % (module, name))
RuntimeError: Model class django.contrib.contenttypes.models.ContentType
doesn't declare an explicit app_label and either isn't in an application
in INSTALLED_APPS or else was imported before its application was loaded
}}}

I understand this isn't a normal situation. But should importing the GFK
field really cause the application loading logic to execute?

--
Ticket URL: <https://code.djangoproject.com/ticket/26023#comment:6>

Reply all
Reply to author
Forward
0 new messages