#27897: Cannot search for foreign key's pk in the admin anymore
-------------------------------+--------------------------------------
Reporter: rm_ | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by rm_:
Old description:
> On django 1.10.6 and python 2.7.9 given this Model and ModelAdmin:
>
> {{{
> class Foo(models.Model):
> name = models.CharField(
> max_length=256
> )
>
> class FooAdmin(admin.ModelAdmin):
> search_fields = ['pk']
> }}}
>
> Each search returns:
>
> {{{
> File "/venv/local/lib/python2.7/site-
> packages/django/contrib/admin/views/main.py" in __init__
> 81. self.queryset = self.get_queryset(request)
> File "/venv/local/lib/python2.7/site-
> packages/django/contrib/admin/views/main.py" in get_queryset
> 359. request, qs, self.query)
> File "/venv/local/lib/python2.7/site-
> packages/django/contrib/admin/options.py" in get_search_results
> 994. 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
> 26. field = opts.get_field(field_name)
> File "/venv/local/lib/python2.7/site-
> packages/django/db/models/options.py" in get_field
> 554. raise FieldDoesNotExist('%s has no field named %r' %
> (self.object_name, field_name))
>
> Exception Type: FieldDoesNotExist at /admin/athome/disciplina/
> Exception Value: Foo has no field named u'pk'
> }}}
>
> Same code works fine in 1.8.16.
New description:
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
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27897#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/061.49c58ab5f7d24ee733196272ceb9d5ba%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.