Opened 9 years ago
Closed 9 years ago
#27883 closed New feature (needsinfo)
"Vertical" polymorphism of models
| Reported by: | Victor Porton | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.10 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
https://docs.djangoproject.com/en/1.10/ref/contrib/contenttypes/ describes what I call "horizontal" polymorphisms of models: an object is identified by both model name and ID.
I want "vertical" polymorphism where an object is identified by its sole ID. (It is in some aspects better, as for vertical polymorphisms such things as unique IDs (and thus one-to-one relations) make sense, which are harder to implement with horizontal polymorphism.)
More precisely, I want PolymorphicModel usable as follows:
class MyModel(models.PolymorphicModel):
klass = models.ForeignKey(ContentType, on_delete=models.CASCADE)
# more fields
Now we can have derived one-to-one models like.
class DerivedModel(MyModel):
# more fields
...
Next we could retrieve the model object using a models.PolymorphicModel class method:
obj = MyModel.polymorphic_retrieve(pk)
This needs two DB read operations: One to get the klass and yet one to read the specific descendant model.
I may attempt to formulate this precisely and to write the code, but I am not particularly strong in Django internals.
It is a very important features which can find many usages.
Change History (3)
comment:1 by , 9 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
| Resolution: | → needsinfo |
|---|---|
| Status: | new → closed |
Design decisions like this need some consensus on the django-developers mailing list is needed, e.g. past thread.
I think there are already a couple of existing third party applications implementing that. Am I missing something?