Opened 6 years ago

Closed 5 years ago

Last modified 3 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: UI/UX:

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 timo 5 years ago.
document case #1

Download all attachments as: .zip

Change History (12)

comment:1 Changed 6 years ago by mrts

  • milestone set to 1.1
  • Needs documentation set
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 6 years ago by mrts

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

  • 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 Changed 6 years ago by mtredinnick

  • milestone 1.1 deleted

comment:5 Changed 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

Changed 5 years ago by timo

document case #1

comment:6 Changed 5 years ago by ramiro

See also #12642.

comment:7 Changed 5 years ago by rctay

  • Has patch set

comment:8 Changed 5 years ago by timo

  • milestone set to 1.2
  • Needs documentation unset
  • Triage Stage changed from Accepted to Ready 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 Changed 5 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(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 Changed 5 years ago by russellm

(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 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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