#16368: Custom sites model overridden by contrib.sites.model.Site
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  briehan.lombaard@…                 |         Status:  reopened
                   Type:  Bug        |      Component:  contrib.sites
              Milestone:             |       Severity:  Normal
                Version:  SVN        |       Keywords:
             Resolution:             |      Has patch:  0
           Triage Stage:  Accepted   |    Needs tests:  0
    Needs documentation:  0          |  Easy pickings:  0
Patch needs improvement:  0          |
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
Changes (by lakin):

 * status:  closed => reopened
 * resolution:  duplicate =>


Comment:

 I'm re-opening this as I feel that I can better explain what is happening
 here.  It is the combination of a few different, but important aspects and
 there may be a few different fixes that are in order.

 First off - from the sites documentation -
 https://docs.djangoproject.com/en/dev/ref/contrib/sites/#requestsite-
 objects  There are parts of django that use the sites framework even if
 it's not installed.  So ultimately, there valid use cases where the sites
 app is NOT installed, but it IS imported.  This is an important
 distinction.  In these case, I feel that it is reasonable that one should
 be able to make a new sites app with a model called Site.

 The above is not a bug, in and of itself.  However, when the sites app is
 imported, its models.py is also imported.  When the models.py is imported
 the ModelBase metaclass runs and prepares this model.  It also caches this
 model for future use so that subsequent imports don't go through the
 costly process of preparing the model a second time.  However, it caches
 the model based on the app_label and model class name, which in this case
 is 'sites.Site'.  When the new, custom app is imported, the same process
 occurs. However - this time around it notices that it already prepared a
 'sites.Site' - so rather than making a new one, it returns a reference to
 the old one.  However, in this particular case it will always the wrong
 reference.  It will always be a reference to the non-used
 django.contrib.sites.models.Site model instead of the custom one.  For
 reference, this is the code that does the caching:
 
https://code.djangoproject.com/browser/django/trunk/django/db/models/base.py#L91

 There are many potential solutions to this - but I'm not sure which is the
 appropriate one.  Ultimately I feel that django's use of app_labels as
 unique across all installed apps is the actual errant decision - but this
 ticket isn't about fixing that.  This ticket is about fixing it so that
 one can use a custom sites.models.Site model when the
 django.contrib.sites.models.Site is not being used.

 I think the appropriate usage would be to update it so that the Meta class
 for django's models only caches models that it has prepared when they are
 part of an installed application.  If they are not part of an installed
 application then they should be ignored.

 You can test this using the same attached example, but this time include
 django.contrib.admin in the INSTALLED_APPS (which imports sites).  Or you
 can import the contenttypes app which also uses it.

 Does that help explain the issue?

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to