Opened 13 years ago

Closed 13 years ago

Last modified 13 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: dev
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

Description

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:
    Site.objects.using(self.connection.alias).update(id=settings.SITE_ID)

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.

  /tmp/django/db/backends/creation.py(239)create_test_db()
-> load_initial_data=False)
  /tmp/django/core/management/__init__.py(166)call_command()
-> return klass.execute(*args, **defaults)
  /tmp/django/core/management/base.py(219)execute()
-> self.validate()
  /tmp/django/core/management/base.py(243)validate()
-> from django.core.management.validation import get_validation_errors
  /tmp/django/core/management/validation.py(3)<module>()
-> from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation
  /tmp/django/contrib/contenttypes/generic.py(14)<module>()
-> from django.contrib.admin.options import InlineModelAdmin, flatten_fieldsets
  /tmp/django/contrib/admin/__init__.py(6)<module>()
-> from django.contrib.admin.sites import AdminSite, site
  /tmp/django/contrib/admin/sites.py(5)<module>()
-> from django.contrib.admin.forms import AdminAuthenticationForm
  /tmp/django/contrib/admin/forms.py(4)<module>()
-> from django.contrib.auth.forms import AuthenticationForm
  /tmp/django/contrib/auth/forms.py(4)<module>()
-> from django.contrib.sites.models import get_current_site
> /tmp/django/contrib/sites/models.py(6)<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 by Aymeric Augustin, 13 years ago

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: https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#the-contenttype-model

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 by Matt McClanahan, 13 years ago

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
False
>>> from django.db.models import get_model
>>> print get_model('sites', 'Site')
None
Last edited 13 years ago by Matt McClanahan (previous) (diff)

comment:3 by Piotr Czachur, 13 years ago

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