Opened 3 years ago

Closed 3 years ago

#33352 closed New feature (duplicate)

ModelAdmin.list_display option should support double-underscore notation to express field-of-associated-model

Reported by: Ofer Nave Owned by: nobody
Component: contrib.admin Version: 4.0
Severity: Normal Keywords:
Cc: Adam Johnson Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Many of the options on ModelAdmin already support double-underscore notation, such as 'ordering' and 'search_fields':

https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields

However, 'list_display' doesn't. This seems like an extremely common use case that everyone would benefit from. For example, imagine I have the following models:

class User(models.Model):
    name = models.CharField(max_length=50)

class CreditCard(models.Model):
    owner = models.ForeignKey(User)
    label = models.CharField(max_length=30)
    limit = models.IntegerField()

And wanted to include the credit card owner's name as a column on the Changelist page for CreditCard. Right now I have to do this:

class CreditCardAdmin(admin.ModelAdmin):
    list_display = [ 'owner_name', 'label', 'limit' ]

    @admin.display(ordering='owner__name', description='Owner')
    def owner_name(self, obj):
        return obj.owner.name

Instead of just doing this:

class CreditCardAdmin(admin.ModelAdmin):
    list_display = [ 'owner__name', 'label', 'limit' ]

BTW - A version of this is already implemented in the django-related-admin package:
https://github.com/PetrDlouhy/django-related-admin/

Change History (2)

comment:1 by Adam Johnson, 3 years ago

Cc: Adam Johnson added

comment:2 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #10743.

Note: See TracTickets for help on using tickets.
Back to Top