Code

Opened 3 years ago

Closed 3 years ago

#15221 closed Bug (fixed)

Show field's verbose name instead of related model name for M2M filterspecs

Reported by: julien Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Normal Keywords:
Cc: charettes Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: yes

Description

Consider the following example:

Model:

from django.db import models
from django.contrib.auth.models import User

class Book(models.Model):
    title = models.CharField(max_length=25)
    year = models.PositiveIntegerField(null=True, blank=True)
    author = models.ForeignKey(User, related_name='books_authored', blank=True, null=True)
    contributors = models.ManyToManyField(User, related_name='books_contributed', blank=True, null=True)

Admin:

from django.contrib import admin

from .models import Book

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'year', 'author')
    list_filter = ('year', 'author', 'contributors')
    
admin.site.register(Book, BookAdmin)

Presumably since #3400 was fixed, M2M filterspecs are using the related model's name as title. This leads the example above to display "By user" instead of "By contributors". This is a bit silly in itself but it could be even more problematic if multiple M2M filters to User were specified at the same time.

I've discussed this with DrMeers (who wrote the patch for #3400). The patch I'm attaching seems to fix the issue but we might have to investigate a bit more to see if there are cases where it wouldn't make sense to show the field's verbose name for a M2M.

The patch doesn't contain tests yet. I'm waiting to get feedback on the latest patch I've posted in #8528 which introduces a new module for filterspecs' tests. If #8529 gets checked in then writing tests for this patch will be trivial.

Attachments (3)

15221_m2m_filterspec_title.diff (1.1 KB) - added by julien 3 years ago.
15221_m2m_filterspec_title.2.diff (4.3 KB) - added by julien 3 years ago.
Patch + tests
15221_m2m_filterspec_title.3.diff (5.0 KB) - added by dmclain 3 years ago.
updated patch to match up against trunk

Download all attachments as: .zip

Change History (16)

Changed 3 years ago by julien

comment:1 Changed 3 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Yes, this problem exists, but no, it isn't related (directly) #3400 specific -- the same problem exists in 1.2.X.

comment:2 Changed 3 years ago by anonymous

  • Needs tests set

comment:3 Changed 3 years ago by russellm

Oops - that was me.

Changed 3 years ago by julien

Patch + tests

comment:4 Changed 3 years ago by julien

  • Needs tests unset

Here's to hoping it makes the 1.3 cut :-)

comment:5 Changed 3 years ago by charettes

  • Cc charettes added

comment:6 Changed 3 years ago by lrekucki

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 3 years ago by patchhammer

  • Easy pickings unset
  • Patch needs improvement set

15221_m2m_filterspec_title.2.diff fails to apply cleanly on to trunk

Changed 3 years ago by dmclain

updated patch to match up against trunk

comment:8 Changed 3 years ago by julien

Oh, you beat me to it. Thanks for updating the patch! ;-)

So yeah, this is related to #15971.

comment:9 Changed 3 years ago by julien

  • UI/UX set

comment:10 Changed 3 years ago by julien

#16711 is a dupe.

comment:11 Changed 3 years ago by anonymous

Here is a workaround to this problem:

http://stackoverflow.com/questions/7199922/django-admin-filter-verbose-name-on-listing-page/

I hope that can be useful to someone.

comment:12 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

comment:11 Changed 3 years ago by julien

  • Resolution set to fixed
  • Status changed from new to closed

In [16991]:

Fixed #15221 -- Made the admin filters on foreign key and m2m relationships display the related field's verbose name instead of that of the related model.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.