#20659 closed Bug (fixed)
Error in the example for Using SingleObjectMixin with ListView
| Reported by: | 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)
Change History (10)
comment:1 by , 12 years ago
| Cc: | added |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
by , 12 years ago
| Attachment: | ticket-20659.diff added |
|---|
comment:2 by , 12 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 , 12 years ago
Baptiste, I've just applied your patch, see PR here: https://github.com/django/django/pull/1318
comment:4 by , 12 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:5 by , 12 years ago
| Triage Stage: | Accepted → 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:7 by , 12 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
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.