#28897: FieldError when performing custom action in Admin Interface IF data is
sorted by an annotated field
-------------------------------------+-------------------------------------
     Reporter:  Colton Hicks         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  contrib.admin        |                  Version:  4.0
     Severity:  Normal               |               Resolution:
     Keywords:  Admin Interface,     |             Triage Stage:
  Custom Action, Annotated Field,    |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by salomvary):

 * status:  closed => new
 * version:  2.0 => 4.0
 * resolution:  needsinfo =>


Comment:

 Hey folks, I've run into this bug both on django 3.x and 4.x. Here is a
 small showcase project:
 https://github.com/salomvary/django_admin_action_bug

 Steps to reproduce:

 - Run the project
 - Sign in to admin
 - Create a few "Things"
 - Go to Home › My_App › Things and select a Thing using the checkboxes
 - Go to Actions > "Update Something"
 - Click "Go"


 You will see the following error page:


 {{{
 FieldError at /admin/my_app/thing/

 Cannot resolve keyword 'other_things_count' into field. Choices are: id,
 is_something, other_things
 }}}

 Few things to note:

 - Removing queryset.order_by eliminates the problem
 - Removing annotations that involve joins also eliminates the problem

 Stack trace:


 {{{
 Internal Server Error: /admin/my_app/thing/
 Traceback (most recent call last):
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/core/handlers/exception.py", line 47, in inner
     response = get_response(request)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/core/handlers/base.py", line 181, in _get_response
     response = wrapped_callback(request, *callback_args,
 **callback_kwargs)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/contrib/admin/options.py", line 622, in wrapper
     return self.admin_site.admin_view(view)(*args, **kwargs)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/utils/decorators.py", line 130, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/views/decorators/cache.py", line 56, in
 _wrapped_view_func
     response = view_func(request, *args, **kwargs)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/contrib/admin/sites.py", line 236, in inner
     return view(request, *args, **kwargs)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/utils/decorators.py", line 43, in _wrapper
     return bound_method(*args, **kwargs)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/utils/decorators.py", line 130, in _wrapped_view
     response = view_func(request, *args, **kwargs)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/contrib/admin/options.py", line 1736, in
 changelist_view
     response = self.response_action(request,
 queryset=cl.get_queryset(request))
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/contrib/admin/options.py", line 1417, in
 response_action
     response = func(self, request, queryset)
   File "/Users/mrc/Projects/django_admin_action_bug/my_app/admin.py", line
 9, in update_something
     queryset.update(is_something=True)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/query.py", line 790, in update
     rows = query.get_compiler(self.db).execute_sql(CURSOR)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 1591, in
 execute_sql
     cursor = super().execute_sql(result_type)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 1189, in
 execute_sql
     sql, params = self.as_sql()
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 1526, in as_sql
     self.pre_sql_setup()
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 1626, in
 pre_sql_setup
     super().pre_sql_setup()
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 60, in
 pre_sql_setup
     order_by = self.get_order_by()
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 389, in
 get_order_by
     for expr, is_ref in self._order_by_pairs():
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 373, in
 _order_by_pairs
     yield from self.find_ordering_name(
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 767, in
 find_ordering_name
     field, targets, alias, joins, path, opts, transform_function =
 self._setup_joins(pieces, opts, alias)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/compiler.py", line 807, in
 _setup_joins
     field, targets, opts, joins, path, transform_function =
 self.query.setup_joins(pieces, opts, alias)
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/query.py", line 1604, in setup_joins
     path, final_field, targets, rest = self.names_to_path(
   File "/Users/mrc/Projects/django_admin_action_bug/.venv/lib/python3.9
 /site-packages/django/db/models/sql/query.py", line 1522, in names_to_path
     raise FieldError("Cannot resolve keyword '%s' into field. "
 django.core.exceptions.FieldError: Cannot resolve keyword
 'other_things_count' into field. Choices are: id, is_something,
 other_things
 [09/Dec/2021 17:00:45] "POST /admin/my_app/thing/ HTTP/1.1" 500 154267

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28897#comment:4>
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/066.504228faf6a51226ffc590aa6936d114%40djangoproject.com.

Reply via email to