Wednesday, May 2, 2012

GET parameters and custom admin query filters

As per 'https://docs.djangoproject.com/en/1.3/releases/1.2.4/
#restricted-filters-in-admin-interface
' to be able to create GET
parameters on the fly one has to add it to the
`ModelAdmin.list_filter` setting.

That's what I have been doing however on my latest project I am using
a custom admin filter that returns active entires only so my GET
filter queries are now throwing:

SuspiciousOperation at /admin/time_sheets/timesheetentry/
Filtering by project__company__id__exact not allowed

I have set the custom's filter parameter_name = 'project__company__id'
or `project__company` but the result is the same. Is this a bug? how
does one list `active__company` so that GET parameters work? As a work
around for now I added both my custom filter and 'project__company' to
the `list_filter` setting.

# ./admin.py
class TimeSheetEntryAdmin(admin.ModelAdmin):
...
list_filter = ['employee', 'status', ActiveCompaniesFilter, ...,
'project__company`]

# ./admin_filters.py
...
class ActiveCompaniesFilter(SimpleListFilter):
title = _('active companies')
parameter_name = 'project__company__id'
def lookups(self, request, model_admin):
lookup_list = Company.objects.active().values_list('id',
'name').distinct()
return lookup_list

def queryset(self, request, queryset):
if self.value():
return queryset.filter(project__company=self.value())

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment