Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#16259 closed Cleanup/optimization (worksforme)

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

Reported by: zimnyx 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

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.

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by aaugustin

  • Component changed from Testing framework to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from BaseDatabaseCreation.create_test_db() requires unexistant table. to State more clearly that django.contrib.ccontenttypes depends on django.contrib.sites
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to Cleanup/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 Changed 3 years ago by mattmcc

  • Resolution set to worksforme
  • Status changed from new to closed

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 3 years ago by mattmcc (previous) (diff)

comment:3 Changed 3 years ago by zimnyx

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.