I have a main class with a category property and several subclasses. I’d like to set the default category for each subclass. For example:
class BaseAd(models.Model):
CATEGORY_CHOICES = ((1, 'Zeta'), (2, 'Sigma'), (3, 'Omega'),)
category = models.IntegerField(choices=CATEGORY_CHOICES)
...
class SigmaAd(BaseAd):
additional_prop = models.URLField()
category = models.IntegerField(default=2, editable=False)
Of course, my example is not functional due to a FieldError. How does one go about “overriding” the property value? Or is it a function of the admin I should be focusing on?
You’ll have to override it in the
__init__()method of the child. Check the keyword arguments passed to see ifcategoryhas been given a value, and if not set it to2before passing it to the parent.