Code

Opened 2 years ago

Closed 20 months ago

Last modified 20 months ago

#17789 closed Bug (needsinfo)

when i use a custom FieldListFilter class for a given field.

Reported by: Kidwind Owned by: julien
Component: contrib.admin Version: 1.4
Severity: Normal Keywords: FieldListFilter
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by aaugustin)

when i use a custom FieldListFilter class for a given field.

list_filter = ('Site', ('RelationModel__Field', MyCustomFieldListFilter),)

i get "Filtering by RelationModel__Field__exact not allowed"

Attachments (2)

options.py.diff (616 bytes) - added by Kidwind 2 years ago.
17789.diff (641 bytes) - added by aaugustin 2 years ago.

Download all attachments as: .zip

Change History (10)

Changed 2 years ago by Kidwind

comment:1 Changed 2 years ago by aaugustin

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Fixed formatting -- please use preview.

comment:2 Changed 2 years ago by aaugustin

  • Description modified (diff)

comment:3 Changed 2 years ago by julien

  • Owner changed from nobody to julien
  • Triage Stage changed from Unreviewed to Accepted

Changed 2 years ago by aaugustin

comment:4 Changed 2 years ago by aaugustin

I've regenerated the patch from the root for clarity.

comment:5 Changed 2 years ago by julien

  • Resolution set to needsinfo
  • Status changed from new to closed
  • Triage Stage changed from Accepted to Unreviewed

As stated in the documentation, if an element of list_filter is a tuple, then the first element of that tuple must be a field name. Your example ('RelationModel__Field') isn't just a field name — it spans relationships. I'm not saying that there can't be any valid use case for this, but right now it seems like a mis-use of the API.

Please reopen this ticket if you can describe a valid use case for using a field name spanning relationships inside that tuple. If you do, then providing some sample code would be useful to understand your use case. Thank you.

comment:6 Changed 21 months ago by KyleMac

  • Resolution needsinfo deleted
  • Status changed from closed to reopened
  • Version changed from 1.3 to 1.4

I've also run into this problem.

My situation is that I have the same model appearing in list_display and list_filter but through different relationships. List_display is showing the current supplier whereas list_filter can be used to find all items supplied by a specified supplier in the past.

This creates an issue where I have a column named "supplier" and a filter named "supplier" but the filter is not the same as the column. To clarify this I wanted to create a SimpleListFilter and set the title attribute to something more descriptive but this raises a SuspiciousOperation and as far as I can tell there is no other way to change the display title for filters.

If this is to be considered a feature and not a bug then the documentation needs to be clarified. Julien says that the documentation states that the first element must be a "field name", but two bullet points up the docs call company__name a "field name".

comment:7 Changed 20 months ago by prestontimmons

  • Resolution set to needsinfo
  • Status changed from reopened to closed

KyleMac -

I read through your comment, but I'm not sure the issue you describe is the same as the ticket subject--which is about letting fields in custom filters span relations.

If you think you have a use case for this, please post a code sample to show how changing the current behavior would help.

comment:8 Changed 20 months ago by KyleMac

My issue is that I wanted to use custom filters to change the titles of filters to clarify their purpose. However you can't do this with filters that span relationships because of the subject of this ticket.

There is currently no other way to change the title of a filter other than using verbose_name on the field itself but that has a much wider effect.

What I was trying to do was something like the following:

class PastSupplierFilter(RelatedFieldListFilter):
    def __init__(self, *args, **kwargs):
        super(PastSupplierFilter, self).__init__(*args, **kwargs)
        self.title = _('past supplier')


class ReorderAdmin(admin.ModelAdmin):
    list_filter = (
        ('product__suppliers', PastSupplierFilter),
    )

A better solution but a more complex change would be to allow list_filter to accept a third value as the title of the filter. Django currently assumes that the name of a field and the purpose/effect of a filter are the same, which is not always the case especially when spanning relationships.

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.