Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#20659 closed Bug (fixed)

Error in the example for Using SingleObjectMixin with ListView

Reported by: tudor.prodan@… Owned by: Susan Tan
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 Baptiste Mispelon 11 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by Baptiste Mispelon, 11 years ago

Cc: bmispelon@… added
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

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.

by Baptiste Mispelon, 11 years ago

Attachment: ticket-20659.diff added

comment:2 by Baptiste Mispelon, 11 years ago

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 by Susan Tan, 11 years ago

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

comment:4 by Susan Tan, 11 years ago

Owner: changed from nobody to Susan Tan
Status: newassigned

comment:5 by Tim Graham, 11 years ago

Triage Stage: AcceptedReady 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 by tudor.prodan@…, 11 years ago

I've tested it, and it works.

comment:7 by Baptiste Mispelon <bmispelon@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

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 by Baptiste Mispelon <bmispelon@…>, 11 years ago

In e03a88ba217006fbd7618e3f836c2f6210638aaf:

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

Backport of 88de53d4a86548016f245a1413b856aa334bc737 from master.

comment:9 by Baptiste Mispelon <bmispelon@…>, 10 years ago

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.

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