#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)
Change History (12)
comment:1 by , 16 years ago
comment:2 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
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 , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:5 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:6 by , 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 , 16 years ago
comment:8 by , 16 years ago
comment:10 by , 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.
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.