[Django] #30699: Remove `PublisherDetail.get_context_data()` from documentation

6 views
Skip to first unread message

Django

unread,
Aug 11, 2019, 12:21:24 PM8/11/19
to django-...@googlegroups.com
#30699: Remove `PublisherDetail.get_context_data()` from documentation
------------------------------------------------+------------------------
Reporter: Davit Gachechiladze | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
The following code snippet is from
[https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/]

{{{#!python
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)`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30699>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 11, 2019, 1:27:01 PM8/11/19
to django-...@googlegroups.com
#30699: Remove `PublisherDetail.get_context_data()` from documentation
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Davit Gachechiladze):

* owner: nobody => Davit Gachechiladze
* status: new => assigned


Old description:

> The following code snippet is from
> [https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/]
>
> {{{#!python
> 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)`.

New description:

{{{#!python
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)`.

[https://github.com/django/django/pull/11658]

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30699#comment:1>

Django

unread,
Aug 12, 2019, 4:03:37 AM8/12/19
to django-...@googlegroups.com
#30699: Remove `PublisherDetail.get_context_data()` from documentation
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: closed
Component: Documentation | Version: 2.2
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: assigned => closed
* resolution: => wontfix


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30699#comment:2>

Django

unread,
Aug 12, 2019, 4:11:13 AM8/12/19
to django-...@googlegroups.com
#30699: Remove `PublisherDetail.get_context_data()` from documentation
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: closed
Component: Documentation | Version: 2.2
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Davit Gachechiladze):

Replying to [comment:2 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30699#comment:3>

Reply all
Reply to author
Forward
0 new messages