Johnny 5 Johnny 5 - 1 month ago 11x
Python Question

In Django - Model Inheritance - Does it allow you to override a parent model's attribute?

I'm looking to do this:

class Place(models.Model):
name = models.CharField(max_length=20)
rating = models.DecimalField()

class LongNamedRestaurant(Place): # Subclassing `Place`.
name = models.CharField(max_length=255) # Notice, I'm overriding `` to give it a longer length.
food_type = models.CharField(max_length=25)

This is the version I would like to use (although I'm open to any suggestion):

Is this supported in Django? If not, is there a way to achieve similar results?


No, it is not:

Field name “hiding” is not permitted

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.