Opened 16 years ago
Closed 16 years ago
#9317 closed (wontfix)
Return content type of child class when parent passed to ContentType.objects.get_for_model
Reported by: | Alexander Artemenko | Owned by: | nobody |
---|---|---|---|
Component: | Contrib apps | Version: | 1.0 |
Severity: | Keywords: | contenttype | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
There is a problem during inheritance, if I have:
class Parent(models.Model): pass class Child(Parent): pass class Child2(Parent): pass
Next create objects of Child and Child2 types.
After that for each of Parent.objects.all() I want to receive it's ContentType. Obviously, that in case of this inheritance, I need to get ContentType of Child or Child2 instead of Parent.
My patch add this 'virtual' behaviour to get_for_model method. Also it includes two unittests for this behaviour.
Attachments (1)
Change History (2)
by , 16 years ago
Attachment: | 0001-Right-content-type-if-parent-object-if-given-and-ac.patch added |
---|
comment:1 by , 16 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
I'm a strong -1 on this for reasons that have already been discussed a lot on both django-dev and django-users. This is related to why querying a parent model shouldn't automatically cast to the descendent instances (which aren't necessarily children, but could be grand-children or even further removed).
Firstly,
get_for_model()
returns the content type for the model you pass in. You're trying to change that to return the content type for some other, related model (the instance you pass in *is* aParent
, it's not down-cast to be one of the child types, so the content type is the content type ofParent
). For everybody, not just your call.Secondly, this is a very expensive operation. If a parent model has 3 children, each of which have 3 children of their own, you've just executed at least 12 database lookups (possibly more if there are other
OneToOneFields
on the models -- you're heavily misusing_collect_related()
here). Thirdly, there's the implementation issue that nothing outside the Model class should be using a method starting with an underscore.There's probably a useful utility set of functions that somebody could build (as a third-party application initially) that does things like descendent content types and down-casting to the right child, automatically looks for a field such as child_type, etc. But those utilities shouldn't initially be targeted at Django's core, since they don't need to modify anything there. Keep in mind the multi-layer, multi-child inheritance situation, since these things start to scale very poorly once you build up any type of significant hierarchy.
So thanks for writing a patch, but this isn't appropriate as a regular use thing in core. There are too many drawbacks.