Opened 4 weeks ago

Closed 2 weeks ago

#29519 closed Bug (needsinfo)

Django admin actions: _selected_action disregarded when `difference` is applied to queryset via filters

Reported by: Andreas Galazis Owned by: nobody
Component: contrib.admin Version: 1.11
Severity: Normal Keywords:
Cc: Andreas Galazis Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Andreas Galazis)

I have an admin with filters where one of them is:

Code highlighting:

class NegatedMyFiledFilter(MyFieldListFilter):
    title = ('excluded Fields')
    parameter_name = 'nonField'

    def queryset(self, request, queryset):
        if self.values():
            queryset_to_negate = super(
                NegatedMyFieldFilter, self
            ).queryset(request, queryset)
            return queryset.difference(queryset_to_negate)

Where MyFieldListFilter is a filter that extends admin.SimpleListFilter
When the above filter is applied(alone or in combination with others) _selected_action is not respected when applying an action and as a result actions get applied to all records, not just the selected ones
As a hack I ended up doing in one of my custom actions (although this is applicable to all actions including build in ones like delete):

Code highlighting:

  qs_ids = queryset.values_list('pk', flat=True)
  selected_ids = request.POST.getlist('_selected_action',qs_ids)
  return TimeSheet.objects.filter(
      pk__in=qs_ids,
  ).filter(pk__in=selected_ids).update(my_flag=True)

but I was expecting the framework to do the second filtering for me
The above happens in version 1.11.13

Change History (5)

comment:1 Changed 4 weeks ago by Andreas Galazis

Cc: Andreas Galazis added
Component: Uncategorizedcontrib.admin
Description: modified (diff)
Version: 2.01.11

comment:2 Changed 4 weeks ago by Andreas Galazis

Description: modified (diff)

comment:3 Changed 4 weeks ago by Andreas Galazis

Type: UncategorizedBug

comment:4 Changed 3 weeks ago by Tim Graham

Can you explain why Django is at fault and propose a fix?

comment:5 Changed 2 weeks ago by Carlton Gibson

Resolution: needsinfo
Status: newclosed

Hi Andreas.

I can't see the issue with what you've provided here. Can you please create a test case or a minimal project that reproduces the issue?

At that point we'll be able to assess if there's a genuine issue here, or whether it's something else that's going on.

Thanks!

Note: See TracTickets for help on using tickets.
Back to Top