Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#9994 closed Uncategorized (fixed)

list_filter ignores to_field

Reported by: Jason Zylks Owned by: Malcolm Tredinnick
Component: contrib.admin Version: 1.0
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

When using the to_field option in a foreign key field, and then filtering on that field in the admin interface, the filter attempts to match the primary key with the values taken from the to_field.

For Example:

class Department(models.Model):
	code = models.CharField(max_length=4, unique=True)
	description = models.CharField(max_length=50, blank=True, null=True)

class Employee(models.Model):
	department = models.ForeignKey(Department, to_field="code")
	name = models.CharField(max_length=100)

...

class EmployeeAdmin(admin.ModelAdmin):
	list_display = ['name', 'department']
	list_filter = ['department']

...

Suppose there is a department, 'FINC' with id 1; the filter that is generated is department__id__exact=FINC rather than department__code__exact=FINC or department__id__exact=1.

Attachments (1)

to_field.diff (1.5 KB ) - added by graveyboat 13 years ago.
patch for Django 1.3.1

Download all attachments as: .zip

Change History (12)

comment:1 by Jason Zylks, 16 years ago

I've found a possible fix for this:

filterspecs.py, Line 61

Changing

self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to._meta.pk.name)

to

self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.get_related_field().attname)

fixes the problem for me.

comment:2 by Malcolm Tredinnick, 16 years ago

Owner: changed from nobody to Malcolm Tredinnick
Status: newassigned

This isn't a complete fix, since if the related field points to another model (chained foreign keys) and so on, I suspect it ends up returning an object and not a pk value. This problem exists in a few places were we're trying to retrieve the "pk value" (really meaning the representative value for the related column) and I keep meaning to fix it holistically in the model class.

Assigning to me so that I have a bug to track that.

comment:3 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:4 by Malcolm Tredinnick, 16 years ago

#10243 is another manifestation of this issue, I suspect.

comment:5 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [10139]) Fixed #9994 -- Fixed admin filtering when to_field is used on relations.

Thanks to jzylks for diagnosing this one.

comment:6 by Luca Lenardi, 16 years ago

After updating Django 1.0.X with this fix, It seems impossible to access the flat-pages list. It generates the following exception:

location url: http://localhost:8000/admin/flatpages/flatpage/

return:

Environment:

Request Method: GET
Request URL: http://localhost:8000/admin/flatpages/flatpage/
Django Version: 1.0.3 pre-alpha SVN-10163
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.flatpages',
 'django_extensions',
 'logging',
 'sorl.thumbnail',
 'bloom.device']
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.csrf.middleware.CsrfMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
 'django.middleware.transaction.TransactionMiddleware',
 'bloom.device.middleware.DeviceDetectMiddleware')


Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
  91.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py" in root
  158.                 return self.model_page(request, *url.split('/', 2))
File "/usr/lib/python2.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py" in model_page
  177.         return admin_obj(request, rest_of_url)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in __call__
  189.             return self.changelist_view(request)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py" in changelist_view
  651.                 self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/main.py" in __init__
  70.         self.filter_specs, self.has_filters = self.get_filters(request)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/views/main.py" in get_filters
  78.                 spec = FilterSpec.create(f, request, self.params, self.model, self.model_admin)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/filterspecs.py" in create
  29.                 return factory(f, request, params, model, model_admin)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/filterspecs.py" in __init__
  61.         rel_name = f.rel.get_related_field().name

Exception Type: AttributeError at /admin/flatpages/flatpage/
Exception Value: 'ManyToManyRel' object has no attribute 'get_related_field'

Maybe this problem is not related to this fix, so I leave the ticket closed.

comment:7 by Russell Keith-Magee, 16 years ago

@lucalenardi - You appear to be correct; changeset [10140] introduced a regression into 1.0.X. This was corrected in [10170] for trunk; I'll merge down the fix for 1.0.X.

comment:8 by Russell Keith-Magee, 16 years ago

(In [10173]) [1.0.X] Fixed a breakage with ManyToManyFields in admin caused by r10140. Refs #9994.

Merge of r10170 from trunk.

comment:9 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

by graveyboat, 13 years ago

Attachment: to_field.diff added

patch for Django 1.3.1

comment:10 by graveyboat, 13 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

This problem seems to have resurfaced in Django 1.3.1. The uploaded patch solves the issue for me. I don't know if there are more underlying issues as mentioned in ticket:9994#comment:2, but it did the trick for me.

comment:11 by Karen Tracey, 13 years ago

I have opened #17972 to track this regression.

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