(This seems to be a common question based on the "Questions that may already have your answer" list, but none of those has helped me.)
I have a several models with multi-table inheritance.
In admin (and later, in the front-end app), I need to have a list of all things in the base class, and also be able to identify which child (or grandchild) class they belong to.
I am trying to use Inheritance Manager for this. No luck so far.
class Entry(models.Model):
name = models.CharField(max_length=200)
description = models.TextField(max_length=500)
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
slug = models.SlugField(unique=True)
objects = InheritanceManager()
def get_queryset(self, request):
qs = self.model.objects.get_queryset()
ordering = self.get_ordering(request)
if ordering:
qs = qs.order_by(*ordering)
return qs
def __str__ (self):
return self.name + " entry"
class Person(Entity):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
def __str__ (self):
return self.name + " Person"
Adding the string "person" and "Entry" is just a test.
Lists of Entities just show Entry
, even if is also(actually) a person.
I would like to be able to write into Entry.__str__
something that would show the final subclass. That way I could get a list of entries and see:
Bob (Person)
ABC Co. (Organization)
Great Expectations (Book)
I had the same issue, and found hacks to it one way or another. But it never felt clean. I ended up using django-polymorphic...
Hope that helps!