#30699 closed Cleanup/optimization (wontfix)
Remove `PublisherDetail.get_context_data()` from documentation
Reported by: | Davit Gachechiladze | Owned by: | Davit Gachechiladze |
---|---|---|---|
Component: | Documentation | Version: | 2.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
The following code snippet is from https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/
from django.views.generic import ListView from django.views.generic.detail import SingleObjectMixin from books.models import Publisher class PublisherDetail(SingleObjectMixin, ListView): paginate_by = 2 template_name = "books/publisher_detail.html" def get(self, request, *args, **kwargs): self.object = self.get_object(queryset=Publisher.objects.all()) return super().get(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['publisher'] = self.object return context def get_queryset(self): return self.object.book_set.all()
PublisherDetail.get_context_data(self, **kwargs)
is not neccessary here at all, isn't it ? SingleObjectMixin.get_context_data(self, **kwargs)
is responsible to add publisher
key in context
dict, which is called from BaseListView.get(self, request, *args, **kwargs)
.
Change History (3)
comment:1 by , 5 years ago
Description: | modified (diff) |
---|---|
Owner: | changed from | to
Status: | new → assigned |
follow-up: 3 comment:2 by , 5 years ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
comment:3 by , 5 years ago
Replying to Carlton Gibson:
I think the override is serving a purpose here:
We have to think carefully about get_context_data(). Since both SingleObjectMixin and ListView will put things in the context data under the value of context_object_name if it’s set, we’ll instead explicitly ensure the Publisher is in the context data.
Removing it is fragile. (Without it, as soon as I set
context_object_name
my code would break.)
With this discussion kind of topic there's always other/better ways to do things but in context I think the existing example is fine.
Thanks for the input.
I understand. Thanks.
I think the override is serving a purpose here:
Removing it is fragile. (Without it, as soon as I set
context_object_name
my code would break.)With this discussion kind of topic there's always other/better ways to do things but in context I think the existing example is fine.
Thanks for the input.