Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#27897 closed Bug (fixed)

Searching for foreign key's pk in the admin crashes with FieldDoesNotExist

Reported by: rm_ Owned by: Josh Schneier
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by rm_)

On django 1.10.6 and python 2.7.9 given this Model and ModelAdmin:

class Bar(models.Model):
    name = models.CharField(
        max_length=256
    )

class Foo(models.Model):
    name = models.CharField(
        max_length=256
    )
    bar = models.ForeignKey(Bar)

class FooAdmin(admin.ModelAdmin):
    search_fields = ['bar__pk']

Each search returns:

File "/venv/local/lib/python2.7/site-packages/django/contrib/admin/views/main.py" in __init__
  78.         self.queryset = self.get_queryset(request)

File "/venv/local/lib/python2.7/site-packages/django/contrib/admin/views/main.py" in get_queryset
  346.         qs, search_use_distinct = self.model_admin.get_search_results(request, qs, self.query)

File "/venv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in get_search_results
  908.                     if lookup_needs_distinct(self.opts, search_spec):

File "/venv/local/lib/python2.7/site-packages/django/contrib/admin/utils.py" in lookup_needs_distinct
  32.         field = opts.get_field(field_name)

File "/venv/local/lib/python2.7/site-packages/django/db/models/options.py" in get_field
  619.             raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))

Exception Type: FieldDoesNotExist at /admin/app/foo/
Exception Value: Bar has no field named u'pk'

Same code works fine in 1.8.16.

UPDATE: i've updated the test case as searching for the pk of the admin model does not work in 1.8.16 too

Change History (5)

comment:1 by rm_, 7 years ago

Description: modified (diff)
Summary: Cannot search for pk in the admin anymoreCannot search for foreign key's pk in the admin anymore

comment:2 by Tim Graham, 7 years ago

Summary: Cannot search for foreign key's pk in the admin anymoreSearching for foreign key's pk in the admin crashes with FieldDoesNotExist
Triage Stage: UnreviewedAccepted

Looks like a regression in 1.9 caused by d084176cc1273d5faf6f88eedb4c490e961f3a68. I'm hesitant to mark as a release blocker since it when unreported for so long and I think it can be worked around by using __id instead of __pk in search_fields, but when a patch is provided we could consider a backport.

comment:3 by Josh Schneier, 7 years ago

Has patch: set
Owner: changed from nobody to Josh Schneier
Status: newassigned
Version: 1.10master

comment:4 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 6bc4ff36:

Fixed #27897 -- Fixed crash with 'pk' in ModelAdmin.search_filters.

comment:5 by Tim Graham <timograham@…>, 7 years ago

In 0cc9175c:

[1.11.x] Fixed #27897 -- Fixed crash with 'pk' in ModelAdmin.search_filters.

Backport of 6bc4ff36db0d85ac96ca91ae758159ee43836f86 from master

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