Lets say I have two models connected with Multi-table Inheritance
class modelA(models.Model):
name = models.CharField(blank=True,null=True,max_length=200)
description = models.CharField(blank=True,null=True,max_length=200)
class modelB (modelA):
friends = models.CharField(blank=True,null=True,max_length=200)
favourite_food = models.CharField(blank=True,null=True,max_length=200)
When I try to add an object to modelB, I find that although the name and description
fields from the parent model are listed, it’s blank so I have to set a new value to it. From my understanding of the docs on Multi-table Inheritance, I thought the child model will inherit all the fields of the parent’s model and so I only have to set the new fields defined in the child model when creating an object. Is this not the case?
The child models inherit the fields, not the values in the fields. Review the docs for more details and examples - Models | Django documentation | Django
So is it possible to set the values in the fields as well so that essentially it’s a copy of the parent model but just with extra fields. I read the docs but I couldn’t find relating to that.
Okay, I understand that the data will be in a different table so MultiTableInheritance may not be what I want to use then.
so what model inheritance should I use if I want to create a child model that will inherit the fields and values of it’s parent? Then I would just add the new fields in the child model and those will be the only ones I enter when creating an object of the model.
Abstract base classes wouldn’t work as I still want a table for the Parent model and Proxy models are for methods , managers , etc.
That wouldn’t be model inheritance then. That’s just a Foreign Key relationship between two tables.
To clarify this a little bit. When you’re talking about model inheritance, you’re talking about a structural relationship between models. One model logically containing the same structure as another model. It’s not talking about the data within those models.
When you’re looking to establish relationships among the data within the models, that’s where the foreign key concept comes into play. It allows you to identify relationships between specific instances of models to specific instances of other models.