Opened 15 years ago

Closed 14 years ago

Last modified 12 years ago

#10712 closed (fixed)

ModelAdmin.queryset() is undocumented.

Reported by: mrts Owned by: nobody
Component: Documentation Version: 1.0
Severity: Keywords: efficient-admin
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

However, it is extremely useful and advocated by e.g. James in http://www.b-list.org/weblog/2008/dec/24/admin/ .

Attachments (1)

10712.diff (962 bytes ) - added by Tim Graham 14 years ago.
document case #1

Download all attachments as: .zip

Change History (12)

comment:1 by mrts, 15 years ago

milestone: 1.1
Needs documentation: set

Tentatively pushing to 1.1, feel free to push to 1.2.

comment:2 by mrts, 15 years ago

Ideally, the following use cases would be documented:

  • show only objects owned by the logged-in user (by James):
    def queryset(self, request):
        if request.user.is_superuser:
            return Entry.objects.all()
        return Entry.objects.filter(author=request.user)
    
  • defer large fields that would otherwise slow down the changelist view and cause it to consume memory (#10710 needs to be fixed for this to work):
       def queryset(self, request):
           qs = super(...).queryset(request)
           return qs.only('list_display_field_1', 'list_display_field_2').select_related().only('related__field_used_in_string_representation')
    

comment:3 by mrts, 15 years ago

Keywords: efficient-admin added

The second use-case should wait for #10742 and #10733.

The proper incantation should actually be in the lines of the following:

class C(Base):
    name = models.CharField(max_length=10)
    is_published = models.BooleanField() 
    a = models.ForeignKey(A, blank=True, null=True)
    b = models.ForeignKey(B)

class CAdmin(admin.ModelAdmin):
    list_display = ('name', 'a', 'b', 'is_published')
    list_select_related = None

    def queryset(self, request):
        qs = super(CAdmin, self).queryset(request)
        return qs.only('name', 'is_published', 'a__name', 'b__name').select_related('a', 'b') 

comment:4 by Malcolm Tredinnick, 15 years ago

milestone: 1.1

comment:5 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

by Tim Graham, 14 years ago

Attachment: 10712.diff added

document case #1

comment:6 by Ramiro Morales, 14 years ago

See also #12642.

comment:7 by Tay Ray Chuan, 14 years ago

Has patch: set

comment:8 by Tim Graham, 14 years ago

milestone: 1.2
Needs documentation: unset
Triage Stage: AcceptedReady for checkin

would be nice to get this in 1.2 if possible, the second use case above should probably be broken off to a separate ticket

comment:9 by Russell Keith-Magee, 14 years ago

Resolution: fixed
Status: newclosed

(In [13170]) Fixed #10712 -- Added documentation for the queryset() method on ModelAdmin. Thanks to mrts for the report, and timo for the patch.

comment:10 by Russell Keith-Magee, 14 years ago

(In [13176]) [1.1.X] Fixed #10712 -- Added documentation for the queryset() method on ModelAdmin. Thanks to mrts for the report, and timo for the patch.

Backport of r13170 from trunk.

comment:11 by Jacob, 12 years ago

milestone: 1.2

Milestone 1.2 deleted

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