Opened 6 years ago

Last modified 4 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

Description

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 6 years ago by mrts

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

comment:2 Changed 6 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 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 6 years ago by mrts

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

comment:5 Changed 6 years ago by kmtracey

#11848 requested this again.

comment:6 Changed 6 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.list_display.remove('category')
        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('',
            *tuple(
                ((r'%s/$' % categories), self.admin_site.admin_view(
                    curry(self.categories_changelist_view, category=category.pk)))
                for category in Category.objects.all())
            )
        return urls_categories + urls

comment:7 Changed 5 years ago by andybak

  • Cc andy@… added

comment:8 Changed 5 years ago by ubernostrum

  • milestone 1.2 deleted

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

comment:9 Changed 4 years ago by SmileyChris

  • Severity set to Normal
  • Type set to New feature

comment:10 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:11 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

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