I have the following code:
# apps/models.py :
class Parent(models.Model):
name = models.CharField(max_length=80)
def __unicode__(self):
clist = ", ".join([x.__unicode__() for x in self.children.all()])
return self.name + clist
class Child(models.Model):
unit = models.ForeignKey(Parent, related_name='children')
desc = models.CharField(max_length=80)
def __unicode__(self):
return self.desc
class ChildA(Child):
text = models.TextField()
def __unicode__(self):
return self.text[:40]
I have several items of type ChildA
. Why when I ask for a __unicode__()
of the relevant Parent
, the string I get in return is the one generated by the __unicode__()
method of Child
and not the __unicode__()
method of ChildA
?
Updates:
This is standard behavior. Another possible solutions in addition to answers below is an inheritance cast