问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/6897730/how-to-override-a-field-in-the-parent-class