Answer the question
In order to leave comments, you need to log in
How to separate implementation of different model types?
There is a need to create a model that, when created, turns into one of its heirs. That is, during initialization, we indicate exactly what type of this model this instance will be. The problem is that in SQL the field sets are declared for each model. For each type, you can create a separate table. But it is important that the rest of the system communicates with each specific type through a single interface. How to implement this without turning the code into a crutch?
Answer the question
In order to leave comments, you need to log in
Maybe django-polymorphic will work:
https://django-polymorphic.readthedocs.io/en/stabl...
With multi-table inheritance, Django ORM by default only allows you to get all parent and child model records as objects of the parent model class.
The django-polymorphic library allows, by making a request for objects of the parent model, to receive objects of different classes (both parent and child models).
Plus, classes are implemented there that allow you to adapt the Django admin panel to convenient work with polymorphic data.
For example, to have a single subsection in the admin panel for parent and child models with a filter of records by type, when creating, go through an intermediate page with a type selection, and also, let's say, select a type when creating records in inline fields. Moreover, everything is quite optional.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question