#16281: ContentType.get_object_for_this_type using wrong database for creating
object
-------------------------------------+-------------------------------------
Reporter: tfrydrychewicz@… | Owner: poirier
Type: Bug | Status: closed
Component: | Version: SVN
contrib.contenttypes | Resolution: invalid
Severity: Release blocker | Triage Stage: Accepted
Keywords: contenttype, object | Needs documentation: 0
get_object_for_this_type, | Patch needs improvement: 0
database, multiple | UI/UX: 0
Has patch: 1 |
Needs tests: 1 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by poirier):
* cc: poirier (added)
* status: assigned => closed
* resolution: => invalid
Comment:
After studying this, I'm not sure the current code is wrong. I'd want to
see a use case in which Django clearly does the wrong thing to be
convinced otherwise. Please re-open the ticket if there is one.
The proposed fix:
{{{
- return
self.model_class()._base_manager.using(self._state.db).get(**kwargs)
+ return
self.model_class()._base_manager.using(self.model_class().objects.db).get(**kwargs)
}}}
seems to assume that there is exactly one database in which it is correct
to look for instances of a given model, but in Django, it's not required
that all instances of a model be stored in one database.
Applying the fix causes one of the multiple database tests,
test_generic_key_separation(), to fail. This test puts pairs of related
instances in each of the two test databases and ensures that querying for
the related instances works, but after this change, is unable to find the
related instance in the 2nd database correctly, probably because now
get_object_for_this_type() is only looking in the default database.
The test does assume that !ContentType objects exist in every database in
which a model has instances, but that seems reasonable, given the
requirement in Django that related objects be stored in the same database.
!ContentType objects don't actually have !ForeignKeys, but logically
they're still related to the models they represent.
The current code looks for the instance of the requested model in the same
database in which the !ContentType instance being used was found, which
fits in with the "related instances in same database" requirement.
However, in working through all this, I found nothing in the Django
documentation discussing how the content type framework and multiple
databases interact, and perhaps this ticket demonstrates the need for more
documentation on how to correctly use content types when multiple
databases are involved.
--
Ticket URL: <https://code.djangoproject.com/ticket/16281#comment:5>
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.