Opened 16 years ago

Last modified 14 months ago

#10761 assigned New feature

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

Reported by: mrts Owned by: Tom Carrick
Component: contrib.admin Version: dev
Severity: Normal Keywords: efficient-admin
Cc: marcoberi@…, andy@…, Jeongsoo, Park 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 (13)

comment:1 by mrts, 16 years ago

Keywords: efficient-admin added

comment:2 by mrts, 16 years ago

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 by Alex Gaynor, 16 years ago

Triage Stage: UnreviewedAccepted

comment:4 by mrts, 16 years ago

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

comment:5 by Karen Tracey, 15 years ago

#11848 requested this again.

comment:6 by marcob, 15 years ago

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 by Andy Baker, 15 years ago

Cc: andy@… added

comment:8 by James Bennett, 15 years ago

milestone: 1.2

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

comment:9 by Chris Beaven, 14 years ago

Severity: Normal
Type: New feature

comment:10 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:11 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:12 by Jeongsoo, Park, 4 years ago

Cc: Jeongsoo, Park added

comment:13 by Tom Carrick, 14 months ago

Owner: changed from nobody to Tom Carrick
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top