I have parent and child classes in Django model. And I want to fill a field in parent class when initialize child class. Or override this field in child class.
class Parent(models.Model):
type = models.CharField()
class Child(Parent):
type = models.CharField() //Doesn't work
Also trying override init method, but it doesn't work too. How can I accomplish this?
In normal Python class inheritance, it is permissible for a child class to override any attribute from the parent class. In Django, this is not permitted for attributes that are Field instances (at least, not at the moment). If a base class has a field called author, you cannot create another model field called author in any class that inherits from that base class.
You can't. Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With