#29816: Bug fix for ticket #29138
-------------------------------------+-------------------------------------
     Reporter:  Constantino          |                    Owner:  nobody
  Schillebeeckx                      |
         Type:  Bug                  |                   Status:  new
    Component:  contrib.admin        |                  Version:  2.1
     Severity:  Normal               |               Resolution:
     Keywords:  autocomplete_fields  |             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 Constantino Schillebeeckx:

Old description:

> I've put together a
> [https://github.com/ConstantinoSchillebeeckx/django/tree/fix-29138 bug
> fix] for the case when `autocomplete_fields` is used on a foreign-key
> field that has `on_fields` specified; bug originally reported
> [https://code.djangoproject.com/ticket/29138 here].
>
> '''Note''' I've been able to update the current unit tests to work with
> my new code; however one of the new unit tests doesn't pass. The error is
> a bit beyond my knowledge of how Django works and I would appreciate some
> help in fixing it:
>
> {{{
> ======================================================================
> ERROR: test_success
> (admin_views.test_autocomplete_view.AutocompleteJsonViewTests)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File
> "/usr/local/opt/pyenv/versions/3.6.3/lib/python3.6/unittest/case.py",
> line 59, in testPartExecutor
>     yield
>   File
> "/usr/local/opt/pyenv/versions/3.6.3/lib/python3.6/unittest/case.py",
> line 605, in run
>     testMethod()
>   File
> "/Users/constantino/Desktop/django/tests/admin_views/test_autocomplete_view.py",
> line 72, in test_success
>     response =
> AutocompleteJsonView.as_view(**self.as_view_args_to_field)(request)
>   File "/Users/constantino/Desktop/django/django/views/generic/base.py",
> line 68, in view
>     return self.dispatch(request, *args, **kwargs)
>   File "/Users/constantino/Desktop/django/django/views/generic/base.py",
> line 88, in dispatch
>     return handler(request, *args, **kwargs)
>   File
> "/Users/constantino/Desktop/django/django/contrib/admin/views/autocomplete.py",
> line 31, in get
>     self.object_list = self.get_queryset()
>   File
> "/Users/constantino/Desktop/django/django/contrib/admin/views/autocomplete.py",
> line 48, in get_queryset
>     qs, search_use_distinct =
> self.model_admin.get_search_results(self.request, qs, self.term)
>   File
> "/Users/constantino/Desktop/django/django/contrib/admin/options.py", line
> 1026, in get_search_results
>     queryset = queryset.filter(reduce(operator.or_, or_queries))
>   File "/Users/constantino/Desktop/django/django/db/models/query.py",
> line 844, in filter
>     return self._filter_or_exclude(False, *args, **kwargs)
>   File "/Users/constantino/Desktop/django/django/db/models/query.py",
> line 862, in _filter_or_exclude
>     clone.query.add_q(Q(*args, **kwargs))
>   File "/Users/constantino/Desktop/django/django/db/models/sql/query.py",
> line 1263, in add_q
>     clause, _ = self._add_q(q_object, self.used_aliases)
>   File "/Users/constantino/Desktop/django/django/db/models/sql/query.py",
> line 1281, in _add_q
>     current_negated, allow_joins, split_subq)
>   File "/Users/constantino/Desktop/django/django/db/models/sql/query.py",
> line 1287, in _add_q
>     split_subq=split_subq,
>   File "/Users/constantino/Desktop/django/django/db/models/sql/query.py",
> line 1225, in build_filter
>     condition = self.build_lookup(lookups, col, value)
>   File "/Users/constantino/Desktop/django/django/db/models/sql/query.py",
> line 1087, in build_lookup
>     raise FieldError('Related Field got invalid lookup:
> {}'.format(lookup_name))
> django.core.exceptions.FieldError: Related Field got invalid lookup:
> icontains
> }}}

New description:

 I've put together a
 [https://github.com/ConstantinoSchillebeeckx/django/tree/fix-29138 bug
 fix] for the case when `autocomplete_fields` is used on a foreign-key
 field that has `on_fields` specified; bug originally reported
 [https://code.djangoproject.com/ticket/29138 here].

 '''Note''' I've been able to update all unit tests and create some new
 ones, however the original ticket mentions using `to_field_allowed` to
 prevent data leaks. I've tried to implement it (see
 
[https://github.com/ConstantinoSchillebeeckx/django/blob/fix-29138/django/contrib/admin/views/autocomplete.py#L29
 here]), however I can't get it to play nicely with the unit tests. When
 uncommented, the `id_field` isn't properly being considered as
 to_field_allowed. I'm not familiar with this function, so could use some
 help troubleshooting.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29816#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/082.2b34434e97ce367e462115bc757c9deb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to