Opened 8 years ago

Last modified 8 years ago

#27883 closed New feature

"Vertical" polymorphism of models — at Initial Version

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

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 to 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 (0)

Note: See TracTickets for help on using tickets.
Back to Top