#34191: ModelAdmin.search_fields fails for DecimalField and __exact
----------------------------------------------+------------------------
               Reporter:  Klaas van Schelven  |          Owner:  nobody
                   Type:  Uncategorized       |         Status:  new
              Component:  Uncategorized       |        Version:  3.2
               Severity:  Normal              |       Keywords:
           Triage Stage:  Unreviewed          |      Has patch:  0
    Needs documentation:  0                   |    Needs tests:  0
Patch needs improvement:  0                   |  Easy pickings:  0
                  UI/UX:  0                   |
----------------------------------------------+------------------------
 From the docs:

 > If you don’t want to use icontains as the lookup, you can use any lookup
 by appending it the field. For example, you could use exact by setting
 search_fields to ['first_name__exact']

 However, when I do this for any DecimalField, and enter any non-parsable
 (as a Decimal) value in the search box, I get the following:


 {{{
 Internal Server Error: /admin/valuta/redacted/
 Traceback (most recent call last):
   File "..../lib/python3.10/site-
 packages/django/db/models/fields/__init__.py", line 1552, in to_python
     return decimal.Decimal(value)
 decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "..../lib/python3.10/site-
 packages/django/core/handlers/exception.py", line 47, in inner
     response = get_response(request)
   File "..../lib/python3.10/site-packages/django/core/handlers/base.py",
 line 181, in _get_response
     response = wrapped_callback(request, *callback_args,
 **callback_kwargs)
   File "..../lib/python3.10/site-
 packages/django/contrib/admin/options.py", line 616, in wrapper
     return self.admin_site.admin_view(view)(*args, **kwargs)
   File "..../lib/python3.10/site-packages/django/utils/decorators.py",
 line 130, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "..../lib/python3.10/site-
 packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
     response = view_func(request, *args, **kwargs)
   File "..../lib/python3.10/site-packages/django/contrib/admin/sites.py",
 line 232, in inner
     return view(request, *args, **kwargs)
   File "..../lib/python3.10/site-packages/django/utils/decorators.py",
 line 43, in _wrapper
     return bound_method(*args, **kwargs)
   File "..../lib/python3.10/site-packages/django/utils/decorators.py",
 line 130, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "..../lib/python3.10/site-
 packages/django/contrib/admin/options.py", line 1697, in changelist_view
     cl = self.get_changelist_instance(request)
   File "..../lib/python3.10/site-
 packages/django/contrib/admin/options.py", line 736, in
 get_changelist_instance
     return ChangeList(
   File "..../lib/python3.10/site-
 packages/django/contrib/admin/views/main.py", line 99, in __init__
     self.queryset = self.get_queryset(request)
   File "..../lib/python3.10/site-
 packages/django/contrib/admin/views/main.py", line 476, in get_queryset
     qs, search_may_have_duplicates = self.model_admin.get_search_results(
   File "..../lib/python3.10/site-
 packages/django/contrib/admin/options.py", line 1032, in
 get_search_results
     queryset = queryset.filter(reduce(operator.or_, or_queries))
   File "..../lib/python3.10/site-packages/django/db/models/query.py", line
 941, in filter
     return self._filter_or_exclude(False, args, kwargs)
   File "..../lib/python3.10/site-packages/django/db/models/query.py", line
 961, in _filter_or_exclude
     clone._filter_or_exclude_inplace(negate, args, kwargs)
   File "..../lib/python3.10/site-packages/django/db/models/query.py", line
 968, in _filter_or_exclude_inplace
     self._query.add_q(Q(*args, **kwargs))
   File "..../lib/python3.10/site-packages/django/db/models/sql/query.py",
 line 1416, in add_q
     clause, _ = self._add_q(q_object, self.used_aliases)
   File "..../lib/python3.10/site-packages/django/db/models/sql/query.py",
 line 1435, in _add_q
     child_clause, needed_inner = self.build_filter(
   File "..../lib/python3.10/site-packages/django/db/models/sql/query.py",
 line 1288, in build_filter
     return self._add_q(
   File "..../lib/python3.10/site-packages/django/db/models/sql/query.py",
 line 1435, in _add_q
     child_clause, needed_inner = self.build_filter(
   File "..../lib/python3.10/site-packages/django/db/models/sql/query.py",
 line 1370, in build_filter
     condition = self.build_lookup(lookups, col, value)
   File "..../lib/python3.10/site-packages/django/db/models/sql/query.py",
 line 1216, in build_lookup
     lookup = lookup_class(lhs, rhs)
   File "..../lib/python3.10/site-packages/django/db/models/lookups.py",
 line 25, in __init__
     self.rhs = self.get_prep_lookup()
   File "..../lib/python3.10/site-packages/django/db/models/lookups.py",
 line 77, in get_prep_lookup
     return self.lhs.output_field.get_prep_value(self.rhs)
   File "..../lib/python3.10/site-
 packages/django/db/models/fields/__init__.py", line 1565, in
 get_prep_value
     return self.to_python(value)
   File "..../lib/python3.10/site-
 packages/django/db/models/fields/__init__.py", line 1554, in to_python
     raise exceptions.ValidationError(
 django.core.exceptions.ValidationError: ['‘adf’ value must be a decimal
 number.']

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34191>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070184c7e53105-4f6ecd45-c632-45a7-8f25-153c7400cbec-000000%40eu-central-1.amazonses.com.

Reply via email to