D
D
dodo1010001012019-01-04 23:15:35
Django
dodo101000101, 2019-01-04 23:15:35

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

2 answer(s)
R
Ruslan Gilfanov, 2019-01-05
@dodo101000101

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.

A
Antonio Solo, 2019-01-05
@solotony

GenericForeignKey?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question