Re: [Django] #28897: FieldError when performing custom action in Admin Interface IF data is sorted by an annotated field

2021-12-09 Thread Django
#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 Sergey Fedoseev):

 * cc: Sergey Fedoseev (removed)


-- 
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.c0befb9181506c4e61f095d15bbb73cb%40djangoproject.com.


Re: [Django] #28897: FieldError when performing custom action in Admin Interface IF data is sorted by an annotated field

2021-12-09 Thread Django
#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, 

Re: [Django] #28897: FieldError when performing custom action in Admin Interface IF data is sorted by an annotated field

2017-12-28 Thread Django
#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:  closed
Component:  contrib.admin|  Version:  2.0
 Severity:  Normal   |   Resolution:  needsinfo
 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 Tim Graham):

 * status:  new => closed
 * resolution:   => needsinfo


Comment:

 Yes, a more minimal (but complete, with models) example seems needed.

-- 
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.96827de832b954c634b74322d2fd6a90%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28897: FieldError when performing custom action in Admin Interface IF data is sorted by an annotated field

2017-12-11 Thread Django
#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:  2.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 Sergey Fedoseev):

 * cc: Sergey Fedoseev (added)


-- 
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.52fbe4f8b9e4b457e024dcda091d4e04%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28897: FieldError when performing custom action in Admin Interface IF data is sorted by an annotated field

2017-12-11 Thread Django
#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:  2.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
-+-

Comment (by Sergey Fedoseev):

 Could you provide traceback and try minimize your example?

-- 
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.148d6f9a32caa246b3e9152c27b8caf5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


[Django] #28897: FieldError when performing custom action in Admin Interface IF data is sorted by an annotated field

2017-12-05 Thread Django
#28897: FieldError when performing custom action in Admin Interface IF data is
sorted by an annotated field
-+-
   Reporter:  coltonbh   |  Owner:  nobody
   Type:  Bug| Status:  new
  Component: |Version:  2.0
  contrib.admin  |   Keywords:  Admin Interface,
   Severity:  Normal |  Custom Action, Annotated Field,
   Triage Stage: |  Has patch:  0
  Unreviewed |
Needs documentation:  0  |Needs tests:  0
Patch needs improvement:  0  |  Easy pickings:  0
  UI/UX:  0  |
-+-
 A FieldError results if I try to perform a custom a custom action on data
 in the admin interface IF that data is sorted by an annotated field. The
 action is obviously not acting on an annotated field as those fields do
 not exist in the database, but raises a FieldError saying it 'Cannot
 resolve keyword 'field_that_data_is_currently_sorted_by' into field.' and
 then lists the choices for fields (all of which are the raw model fields,
 not the annotated fields in the custom queryset).

 My admin model:

 {{{
 @admin.register(Position)
 class PositionAdmin(admin.ModelAdmin):

 def __init__(self, *args, **kwargs):
 super().__init__(*args, **kwargs)

 def get_queryset(self, request):

 qs = super(PositionAdmin,
 self).get_queryset(request).prefetch_related(
 'orders').select_related(
 'portfolio', 'current_position').select_related(
 'current_position').annotate(
 Count('orders', distinct=True),
 Count('short_requests'),
 total_position_value=ExpressionWrapper(
 F('price') * F('quantity'),
 output_field=IntegerField()),
 diff_shares=Coalesce(
 F('quantity')
 - F('current_position__quantity'),
 F('quantity')),
 diff_value=ExpressionWrapper(
 F('diff_shares') * F('price'),
 output_field=IntegerField()),
 orders_with_errors=Count(
 Case(When(~Q(orders__error=''), then=1))),
 non_accepted_orders=Count(
 Case(When(Q(orders__status=''), then=1))),
 order_today=Count(
 Case(When(
 orders__created_at__gte=_brokerage_today_start(),
 then=1
 return qs

 # Custom Action
 def approve_position_for_trading(self, request, queryset):
 try:
 num_pos_approved = queryset.update(approved=True)
 except FieldError:
 self.message_user(
 request, "You cannot perform actions when you have sorted
 by "
 "this column. Please remove your column sortings and then
 try "
 "your action again.", level=messages.ERROR)
 else:
 if num_pos_approved == 1:
 message_bit = "1 position was was"
 else:
 message_bit = "%s positions were" % num_pos_approved
 self.message_user(
 request, "%s successfully approved for trading." %
 message_bit)
 }}}

 I had to write code to handle the error so that the user will know how to
 proceed. However, this seems like bad behavior. Django can successfully
 perform that action (changing the 'Approved' field to 'True') on the data
 if it is sorted by any core model field, or if sorted by no field at all.
 However, if the data is sorted by a field resulting from the annotation of
 my queryset, then I get the FieldError when I try to perform the action.
 This seems like a bug.

-- 
Ticket URL: 
Django 
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.ee8ade616c8c35460d3405b9ca36de0e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.