Code

Opened 2 years ago

Closed 2 years ago

#17936 closed Bug (fixed)

SimpleListFilter redirects to ?e=1

Reported by: anonymous Owned by: nobody
Component: Documentation Version: 1.4-beta-1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I try create custom admin filter. example in django documentation:

class DecadeBornListFilter(SimpleListFilter):
   ...

But, when I choise "in the eighties" or "in the nineties", I get 302 Redirect to /?e=1

Attachments (0)

Change History (5)

comment:1 Changed 2 years ago by julien

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

Could you please upgrade to the latest revision of trunk and see either if the issue is resolved or if you get a more explicit error message?

Also, have you used the exact example given in the documentation or have you customized it a bit. If you could post the exact code you've used, that would be helpful.

comment:2 Changed 2 years ago by anonymous

admin.py

class LevelListFilter(SimpleListFilter):
    title = u"decade born"
    parameter_name = 'decade'

    #template = "select_filter.html"

    def lookups(self, request, model_admin):
        return (
            ('60s', u"60-e"),
            ('70s', u"70-e"),
            ('80s', u"80-e"),
            ('90s', u"90-e"),
        )

    def queryset(self, request, queryset):
        if self.value() == '60s':
            return queryset.filter(birthday__year__gte=1960,
                                   birthday__year__lte=1969)
        if self.value() == '70s':
            return queryset.filter(birthday__year__gte=1970,
                                   birthday__year__lte=1979)
        if self.value() == '80s':
            return queryset.filter(birthday__year__gte=1980,
                                   birthday__year__lte=1989)
        if self.value() == '90s':
            return queryset.filter(birthday__year__gte=1990,
                                   birthday__year__lte=1999)


class EmployeeAdmin(admin.ModelAdmin):
    list_filter = (LevelListFilter, "sex")

models.py

# -*- coding: utf-8 -*-
from django.db import models

SEX = (
    ("m", u"male"),
    ("w", u"female")
)

class Employee(models.Model):
    firstname = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)
    birthday = models.DateField()
    sex = models.CharField(choices=SEX, max_length=1)

    class Meta(object):
        ordering = ["lastname", "firstname"]

    def __unicode__(self):
        return self.firstname

I downloaded trunk and got the error:

FieldError at /admin/new_in_admin/employee/

Join on field 'birthday' not permitted. Did you misspell 'year' for the lookup type?

If change code to:

if self.value() == '70s':
    return queryset.filter(birthday__year=1970)

OR

if self.value() == '70s':
    return queryset.filter(
        birthday__gte=dt(1970, 1, 1),
        birthday__lte=dt(1979, 12, 31),
    )

all work.

comment:3 Changed 2 years ago by julien

  • Severity changed from Normal to Release blocker

Thank you for providing this code sample.

I confirm that something seems quite wrong here; either in the admin documentation, or in the ORM. I would have imagined that "birthday__year__gte=1960" would be a valid lookup, but maybe that isn't supported by the ORM; if it isn't supported, then the admin documentation sample should be fixed. If it should be supported then this would be a bug in the ORM. I thought this might have been a regression introduced by [17450], but reverting the change from that commit doesn't make this code sample work either.

It's late and my brain is a little fried. As a cautionary measure, I'm marking this ticket as release blocker until we dig out the real nature of this potential bug.

comment:4 Changed 2 years ago by julien

  • Component changed from contrib.admin to Documentation
  • Severity changed from Release blocker to Normal
  • Triage Stage changed from Unreviewed to Accepted

Ok, I've verified with the 1.3.X branch and there is no regression here. The ORM simply does not consider "birthday__year__gte" a valid lookup. So I'll just fix the documentation issue.

Last edited 2 years ago by julien (previous) (diff)

comment:5 Changed 2 years ago by julien

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

In [17772]:

Fixed #17936 -- Fixed a code sample in the admin SimpleListFilter documentation. Thanks to anonymous for the report.

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.