wildDAlex
wildDAlex

Reputation: 387

How to override a field in the parent class

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?

Upvotes: 14

Views: 7473

Answers (1)

Mike Ramirez
Mike Ramirez

Reputation: 10970

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

Upvotes: 16

Related Questions