Code

Opened 10 months ago

Closed 10 months ago

Last modified 3 months ago

#20659 closed Bug (fixed)

Error in the example for Using SingleObjectMixin with ListView

Reported by: tudor.prodan@… Owned by: susan
Component: Documentation Version: 1.5
Severity: Normal Keywords:
Cc: bmispelon@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

In this section:
https://docs.djangoproject.com/en/1.5/topics/class-based-views/mixins/#using-singleobjectmixin-with-listview

The example code has a mistake.

Both SingleObjectMixin.get_object and ListView will use self.get_queryset.
get_queryset is overwritten in the example to always return book_set, hence, get_object will always fail.

get_object does take an optional queryset parameter, which, if passed, will be used instead of self.get_queryset, so the example may work if when calling get_object:

    def get(self, request, *args, **kwargs):
-        self.object = self.get_object()
+        self.object = self.get_object(queryset=self.model.objects)
        return super(PublisherDetail, self).get(request, *args, **kwargs)

Attachments (1)

ticket-20659.diff (2.4 KB) - added by bmispelon 10 months ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 10 months ago by bmispelon

  • Cc bmispelon@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

I believe I'm the one who introduced this issue with commit c6862d57c1e987f0f98a77826d19358b9040bad1.

I think the amount of bugs related to this page shows why you should keep your class-based views simple: it's very hard to anticipate the interactions of mixins which are not meant to work together.

Thanks for catching this.

Changed 10 months ago by bmispelon

comment:2 Changed 10 months ago by bmispelon

  • Has patch set

I'm starting to wonder if it wouldn't be easier to remove the SingleObjectMixin and just go with self.object = get_object_or_404(Publisher, pk=self.kwargs['pk']) in the get method of the view.

I find it much cleaner but it defeats the purpose of this part of the documentation.

In any case, here's the patch that fixes the current documentation.

comment:3 Changed 10 months ago by susan

Baptiste, I've just applied your patch, see PR here: https://github.com/django/django/pull/1318

comment:4 Changed 10 months ago by susan

  • Owner changed from nobody to susan
  • Status changed from new to assigned

comment:5 Changed 10 months ago by timo

  • Triage Stage changed from Accepted to Ready for checkin

@susan: thanks, but I don't think simply creating pull requests from patches is necessary (don't want you to waste your time).

Besides a typo ("explicitly") this looks good (although I haven't tested it).

comment:6 Changed 10 months ago by tudor.prodan@…

I've tested it, and it works.

comment:7 Changed 10 months ago by Baptiste Mispelon <bmispelon@…>

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

In 88de53d4a86548016f245a1413b856aa334bc737:

Fixed #20659 -- Fixed PublisherDetail in CBV topic documentation.

Thanks to tudor.prodan, susan, and Tim Graham for the report and reviews.

comment:8 Changed 10 months ago by Baptiste Mispelon <bmispelon@…>

In e03a88ba217006fbd7618e3f836c2f6210638aaf:

[1.6.x] Fixed #20659 -- Fixed PublisherDetail in CBV topic documentation.

Backport of 88de53d4a86548016f245a1413b856aa334bc737 from master.

comment:9 Changed 3 months ago by Baptiste Mispelon <bmispelon@…>

In 0946aac61d0da2aceafe6179cdb64ccad583d924:

[1.5.x] Fixed #20659 -- Fixed PublisherDetail in CBV topic documentation.

Thanks to tudor.prodan, susan, and Tim Graham for the report and reviews.

Backport of 88de53d4a86548016f245a1413b856aa334bc737 from master.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.