Opened 7 years ago

Last modified 5 years ago

#10761 new New feature

ModelAdmin.queryset() is missing a mechanism for specifying different querysets for changelist and change object views

Reported by: mrts Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: efficient-admin
Cc: marcoberi@…, andy@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


ModelAdmin.queryset() is used both in the changelist and change object views.

Suppose some fields are deferred in the returned queryset to speed up changelist rendering. As a result, the object change view has to perform additional queries for all the deferred fields to pull them in.

The proposed solution is to add another parameter to queryset() that specifies whether a changelist or change object queryset should be returned as follows:

def queryset(self, request, for_change_object_view=False):

Change History (11)

comment:1 Changed 7 years ago by mrts

Keywords: efficient-admin added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 7 years ago by mrts

Looking around a bit more, the same queryset is also used in several other places (e.g. for actions and delete view), so the parameter is suboptimal. A holistic design is needed.

One option would be to amend ModelAdmin as follows:

class ModelAdmin(...):
    def queryset(self, request):

    def queryset_for_change_list(self, request):
        return self.queryset(request)

    def queryset_for_change_object(self, request):
        return self.queryset(request)

    def queryset_for_action(self, request):
        return self.queryset(request)


The queryset_for_... would be used in appropriate places. This is also backwards-compatible.

comment:3 Changed 7 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

comment:4 Changed 7 years ago by mrts

#11019 is in essence a duplicate, i.e. it calls for a broader .queryset() refactor.

comment:5 Changed 7 years ago by Karen Tracey

#11848 requested this again.

comment:6 Changed 7 years ago by marcob

Cc: marcoberi@… added

Besides queryset, I would also like to have an hook to customize, for example, list_display and list_filter in a cleaner way than this (mine ugly code follows):

    def category_changelist_view(self, request, extra_context=None, category=None):
        backup_list_filter = self.list_filter[:]
        backup_list_display = self.list_display[:]
        backup_queryset = self.queryset

        self.list_filter = None
        self.queryset = curry(self.category_queryset, category=category)

        ret = super(PVCataAdmin, self).changelist_view(request, extra_context)

        self.list_filter = backup_list_filter
        self.list_display = backup_list_display
        self.queryset = backup_queryset

        return ret

    def get_urls(self):
        urls = super(CategoryAdmin, self).get_urls()
        urls_categories = patterns('',
                ((r'%s/$' % categories), self.admin_site.admin_view(
                for category in Category.objects.all())
        return urls_categories + urls

comment:7 Changed 7 years ago by Andy Baker

Cc: andy@… added

comment:8 Changed 7 years ago by James Bennett

milestone: 1.2

1.2 is feature-frozen, moving this feature request off the milestone.

comment:9 Changed 5 years ago by Chris Beaven

Severity: Normal
Type: New feature

comment:10 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:11 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

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