﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28897	QuerySet.update() raises FieldError on queryset ordered by an annotated field.	Colton Hicks	David Wobrock	"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."	Bug	closed	Database layer (models, ORM)	4.0	Normal	fixed	Admin Interface, Custom Action, Annotated Field,	Saeed Blanchette David Wobrock	Ready for checkin	1	0	0	0	0	0
