Possible Duplicate:
Can “list_display” in a Django ModelAdmin display attributes of ForeignKey fields?
I want to show some information on the admin list view of a model which comes from another, related model.
class Identity(models.Model):
blocked = models.BooleanField()
...
class Person(models.Model):
modelARef = OneToOneField("Identity", primary_key=True)
descr = models.CharField(max_length=255)
name = models.CharField(max_length=255)
The User should be able to add/edit "Person" on the admin page. As there ist no support for reverse inlining I have to show "Identity" on the admin page and then inline "Person". "Identity" only contains additional information to "Person" which should be visible on the admin page.
Now when I have a admin page for "Identity" how can I show fields from the "Person"-model on the list_display of "Identity"?
regards
EDIT: I can add some functions to "Identity" which query the related "Person" and return the needed value but if I do that there is no possibility to sort that column.
You can use a list_display
to add custom columns. I'd also advise updating the get_queryset()
to make sure the related objects are only fetched on one query, instead of causing a query per row.
class IdentityAdmin(admin.ModelAdmin):
list_display = ('blocked', 'person_name')
def person_name(self, object):
return object.person.name
person_name.short_description = _("Person name")
def get_queryset(self, request):
# Prefetch related objects
return super(IdentityAdmin, self).get_queryset(request).select_related('person')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With