Access The Superclass Of A Django Model
Given a Django Model class Sub(models.Model): name = models.CharField(max_length=100) size_in_inches = models.IntegerField(default=6) class TunaSub(Sub): fish_ingredie
Solution 1:
Since you extend Sub
, name
is also a field of both TunaSub
and MeatballSub
. So you can simply use
def__str__(self):
returnself.name
As a side note, since you are extending a concrete model, you are in fact creating three separate tables in the database (named sub
, tuna_sub
, and meatball_sub
) which are connected via one-to-one relations. If you only want to reuse the field definitions in sub
and not actually create a table for it, use an abstract base model class.
Post a Comment for "Access The Superclass Of A Django Model"