A
A
Andrew2021-04-12 19:08:06
Django
Andrew, 2021-04-12 19:08:06

How to create a model where the fields will be another model and its field?

Friends, the task is as follows:
I need to map another model and field to a text field.

class TextTemplateField(models.Model):
    text_template = models.CharField(max_length=64, unique=True)
    model_name = ???
    field_name = ???


Those. suppose there are models:
class SupplierGroup(models.Model):
    name = models.CharField(max_length=32, unique=True)
class Supplier(models.Model):
    name = models.CharField(max_length=512)
    short_name = models.CharField(max_length=100, unique=True)
    official_short_name = models.CharField(max_length=100, blank=True)
    inn = models.CharField(max_length=12, blank=True)
    kpp = models.CharField(max_length=9, blank=True)
    ogrn = models.CharField(max_length=15, blank=True)

The object of the TextTemplateField class should contain, for example:
obj.text_template = "SupplierGroup_name"
obj.model_name = SupplierGroup
obj.field_name = name

So that in the future I could do something like
model_obj = obj.model_name.filter()
and then
print(model_obj .obj.field_name)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2021-04-13
@G_r_i_n_v_i_c_h

Look towards contenttype framework and generic relation

M
maksam07, 2021-04-12
@maksam07

I didn’t read the question much, but I’m immediately interested in the answer:
is working with foreign keys not suitable?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question