B
B
bbquite2021-09-10 13:07:05
Django
bbquite, 2021-09-10 13:07:05

How to remove a field when inheriting a model?

How to remove an extra field when inheriting a django model, for example:

class Model1(models.Model):
    product = models.ForeignKey('catalog.Product')
    name = models.CharField()
    comment = models.TextField()
    comment_2 = models.TextField()
    comment_3 = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)


class Model2(Model1, models.Model):
    product = None
    comment_4 = models.TextField()


In the Model2 model, the product field is not needed, but everything else is the same, and the model methods are the same. Is there any way to remove this field?

There is an option to create another abstract model and it will inherit both Model1 and Model2, but there may be another way

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stefan, 2021-09-10
@WebDev921

In no way, in this case, do not inherit from Model1, or take out the general in Model3, and inherit from it in Model1 and Model2, but in general it’s better to read what inheritance is in OOP and why it was invented, it’s the same as you inherit from a triangle from a square , like everything is the same, but you don’t need one coordinate, this is fundamentally not the right approach that will lead to many problems in the future

R
RqL, 2021-09-10
@RqL

Create an abstract model with common fields and inherit models from it by adding unique fields.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question