Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 497
wildDAlex Avatar asked Aug 01 '11 11:08

wildDAlex


1 Answers

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

like image 119
Mike Ramirez Avatar answered Nov 15 '22 18:11

Mike Ramirez