Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#16259 closed Cleanup/optimization (worksforme)

State more clearly that django.contrib.ccontenttypes depends on django.contrib.sites

Reported by: Piotr Czachur Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I don't have django.contrib.sites inside my installed apps, and despite that BaseDatabaseCreation.create_test_db() assumes that I have Site model which ends up with exception:

# BaseDatabaseCreation.create_test_db()
from django.db.models import get_model
Site = get_model('sites', 'Site')
if Site is not None and Site.objects.using(self.connection.alias).count() == 1:

Exception: django.db.utils.DatabaseError: relation "django_site" does not exist

Why get_model() returns Site instead of None, despite I don't have sites in my INSTALLED_APPS?
Because contenttypes (which I have in INSTALLED_APPS) does "from django.contrib.sites.models import get_current_site", and in that moment Site model gets registered.

-> load_initial_data=False)
-> return klass.execute(*args, **defaults)
-> self.validate()
-> from import get_validation_errors
-> from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation
-> from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
-> from django.contrib.admin.sites import AdminSite, site
-> from django.contrib.admin.forms import AdminAuthenticationForm
-> from django.contrib.auth.forms import AuthenticationForm
-> from django.contrib.sites.models import get_current_site
> /tmp/django/contrib/sites/<module>()
-> ...

My final question is: is get_model() a good way to determine if some model is really "enabled" inside my project?
Maybe checking INSTALLED_APPS for application_name would be better solution.

Change History (3)

comment:1 Changed 6 years ago by Aymeric Augustin

Component: Testing frameworkDocumentation
Summary: BaseDatabaseCreation.create_test_db() requires unexistant table.State more clearly that django.contrib.ccontenttypes depends on django.contrib.sites
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

The doc for contenttypes say that you must enable sites too, but it's not very clear:

I'm re-qualifying the ticket to improve this doc.

Generally, when app A uses models from app B, those models are "enabled" in your app, even if B is not in INSTALLED_APPS. When this happens, for the sake of clarity, you should add app B to your INSTALLED_APPS.

comment:2 Changed 6 years ago by Matt McClanahan

Resolution: worksforme
Status: newclosed

I can't reproduce this. get_model does indeed return None if the app for a given model is not in INSTALLED_APPS. Further, contenttypes does not depend on sites. The documentation uses the site model as an example of a content type, nothing more.

>>> from django.conf import settings
>>> 'django.contrib.sites' in settings.INSTALLED_APPS
>>> from django.db.models import get_model
>>> print get_model('sites', 'Site')
Last edited 6 years ago by Matt McClanahan (previous) (diff)

comment:3 Changed 6 years ago by Piotr Czachur

My apologies, it was fixed in r16053. I must have used outdated Django trunk.

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