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)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20659>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: bmispelon@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:1>
* has_patch: 0 => 1
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:2>
Comment (by susan):
Baptiste, I've just applied your patch, see PR here:
https://github.com/django/django/pull/1318
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:3>
* owner: nobody => susan
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:4>
* stage: Accepted => Ready for checkin
Comment:
@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).
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:5>
Comment (by tudor.prodan@…):
I've tested it, and it works.
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"88de53d4a86548016f245a1413b856aa334bc737"]:
{{{
#!CommitTicketReference repository=""
revision="88de53d4a86548016f245a1413b856aa334bc737"
Fixed #20659 -- Fixed PublisherDetail in CBV topic documentation.
Thanks to tudor.prodan, susan, and Tim Graham for the report and reviews.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:7>
Comment (by Baptiste Mispelon <bmispelon@…>):
In [changeset:"e03a88ba217006fbd7618e3f836c2f6210638aaf"]:
{{{
#!CommitTicketReference repository=""
revision="e03a88ba217006fbd7618e3f836c2f6210638aaf"
[1.6.x] Fixed #20659 -- Fixed PublisherDetail in CBV topic documentation.
Backport of 88de53d4a86548016f245a1413b856aa334bc737 from master.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:8>
Comment (by Baptiste Mispelon <bmispelon@…>):
In [changeset:"0946aac61d0da2aceafe6179cdb64ccad583d924"]:
{{{
#!CommitTicketReference repository=""
revision="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.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20659#comment:9>