Opened 12 years ago

Closed 12 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

Change History (5)

comment:1 by Julien Phalip, 12 years ago

Type: UncategorizedBug

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

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

Severity: NormalRelease 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 by Julien Phalip, 12 years ago

Component: contrib.adminDocumentation
Severity: Release blockerNormal
Triage Stage: UnreviewedAccepted

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 12 years ago by Julien Phalip (previous) (diff)

comment:5 by Julien Phalip, 12 years ago

Resolution: fixed
Status: newclosed

In [17772]:

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

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