Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#17789 closed Bug (needsinfo)

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

Reported by: Kidwind Owned by: Julien Phalip
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 Aymeric Augustin)

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 13 years ago.
17789.diff (641 bytes ) - added by Aymeric Augustin 13 years ago.

Download all attachments as: .zip

Change History (10)

by Kidwind, 13 years ago

Attachment: options.py.diff added

comment:1 by Aymeric Augustin, 13 years ago

Description: modified (diff)

Fixed formatting -- please use preview.

comment:2 by Aymeric Augustin, 13 years ago

Description: modified (diff)

comment:3 by Julien Phalip, 13 years ago

Owner: changed from nobody to Julien Phalip
Triage Stage: UnreviewedAccepted

by Aymeric Augustin, 13 years ago

Attachment: 17789.diff added

comment:4 by Aymeric Augustin, 13 years ago

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

comment:5 by Julien Phalip, 13 years ago

Resolution: needsinfo
Status: newclosed
Triage Stage: AcceptedUnreviewed

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 by KyleMac, 12 years ago

Resolution: needsinfo
Status: closedreopened
Version: 1.31.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 by Preston Timmons, 12 years ago

Resolution: needsinfo
Status: reopenedclosed

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 by KyleMac, 12 years ago

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.

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