[Django] #30698: `BaseDetailView` and `SingleObjectMixin` optimization.

7 views
Skip to first unread message

Django

unread,
Aug 11, 2019, 5:12:59 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
------------------------------------------------+------------------------
Reporter: Davit Gachechiladze | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Generic views | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

**Hi,,,

In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line like
`context = self.get_context_data(object=self.object)`. Why should we pass
`object=self.object` ? It's redundant (IMHO). I think, it's better idea to
implement `SingleObjectMixin.get_context_data` and `BaseDetailView.get`
like this. Code below
**
{{{#!python
class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

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

Django

unread,
Aug 11, 2019, 5:16:43 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------

Reporter: Davit Gachechiladze | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Generic views | 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):

* has_patch: 0 => 1


Old description:

New description:

{{{#!python
class SingleObjectMixin(ContextMixin):

**Hi,,,

[https://github.com/gachdavit/django/pull/1]

--

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

Django

unread,
Aug 11, 2019, 5:18:52 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------

Reporter: Davit Gachechiladze | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Generic views | 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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> [https://github.com/gachdavit/django/pull/1]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

[https://github.com/gachdavit/django/pull/1]

--

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

Django

unread,
Aug 11, 2019, 5:31:53 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned

Component: Generic views | 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


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

Django

unread,
Aug 11, 2019, 5:37:32 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | 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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement
> `SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
> Code below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>
> Also, `SingleObjectMixin` will be implemented in a same fashion as
> `MultipleObjectMixin` is this moment.
>

> [https://github.com/gachdavit/django/pull/1]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:4>

Django

unread,
Aug 11, 2019, 5:57:51 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | 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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement
> `SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
> Code below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>
> Also, `SingleObjectMixin` will be implemented in a same fashion as
> `MultipleObjectMixin` is this moment.

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:5>

Django

unread,
Aug 11, 2019, 6:01:16 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | 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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement
> `SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
> Code below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>
> Also, `SingleObjectMixin` will be implemented in a same fashion as
> `MultipleObjectMixin` is this moment.
>

> [https://github.com/django/django/pull/11653]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:6>

Django

unread,
Aug 11, 2019, 6:08:13 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | 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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement
> `SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
> Code below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>
> Also, `SingleObjectMixin` will be implemented in a same fashion as
> `MultipleObjectMixin` is this moment.

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:7>

Django

unread,
Aug 11, 2019, 7:17:26 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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):

* version: 2.2 => master


--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:8>

Django

unread,
Aug 11, 2019, 9:39:59 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement
> `SingleObjectMixin.get_context_data` and `BaseDetailView.get` like this.
> Code below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>
> Also, `SingleObjectMixin` will be implemented in a same fashion as
> `MultipleObjectMixin` is this moment.
>

> [https://github.com/django/django/pull/11654]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement
`SingleObjectMixin.get_context_data(self, *, object=None, **kwargs)` and
`BaseDetailView.get(self, request, *args, **kwargs)` like this. Code below

{{{#!python
class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:9>

Django

unread,
Aug 11, 2019, 9:40:38 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement

> `SingleObjectMixin.get_context_data(self, *, object=None, **kwargs)` and
> `BaseDetailView.get(self, request, *args, **kwargs)` like this. Code
> below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>
> Also, `SingleObjectMixin` will be implemented in a same fashion as
> `MultipleObjectMixin` is this moment.
>

> [https://github.com/django/django/pull/11654]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement
`SingleObjectMixin.get_context_data(self, **kwargs)` and


`BaseDetailView.get(self, request, *args, **kwargs)` like this. Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin` will be implemented in a same fashion as
`MultipleObjectMixin` is this moment.

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:10>

Django

unread,
Aug 11, 2019, 10:03:55 AM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement

> `SingleObjectMixin.get_context_data(self, **kwargs)` and
> `BaseDetailView.get(self, request, *args, **kwargs)` like this. Code
> below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>
> Also, `SingleObjectMixin` will be implemented in a same fashion as
> `MultipleObjectMixin` is this moment.
>

> [https://github.com/django/django/pull/11654]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data(self, **kwargs)` and
`BaseDetailView.get(self, request, *args, **kwargs)` like this. Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin.get_context_data(self, *, object=None, **kwargs)`


will be implemented in a same fashion as

`MultipleObjectMixin.get_context_data(self, *, object_list=None,
**kwargs)` is this moment.

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:11>

Django

unread,
Aug 11, 2019, 1:00:54 PM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement

> `SingleObjectMixin.get_context_data(self, **kwargs)` and
> `BaseDetailView.get(self, request, *args, **kwargs)` like this. Code
> below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>

> Also, `SingleObjectMixin.get_context_data(self, *, object=None,
> **kwargs)` will be implemented in a same fashion as


> `MultipleObjectMixin.get_context_data(self, *, object_list=None,
> **kwargs)` is this moment.
>
> [https://github.com/django/django/pull/11654]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data(self, **kwargs)` and
`BaseDetailView.get(self, request, *args, **kwargs)` like this. Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin.get_context_data(self, *, object=None, **kwargs)`


will be implemented in a same fashion as

`MultipleObjectMixin.get_context_data(self, *, object_list=None,
**kwargs)` is this moment.

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:12>

Django

unread,
Aug 11, 2019, 1:06:37 PM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement

> `SingleObjectMixin.get_context_data(self, **kwargs)` and
> `BaseDetailView.get(self, request, *args, **kwargs)` like this. Code
> below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>

> Also, `SingleObjectMixin.get_context_data(self, *, object=None,
> **kwargs)` will be implemented in a same fashion as


> `MultipleObjectMixin.get_context_data(self, *, object_list=None,
> **kwargs)` is this moment.
>
> [https://github.com/django/django/pull/11656]

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data(self, **kwargs)` and
`BaseDetailView.get(self, request, *args, **kwargs)` like this. Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin.get_context_data(self, *, object=None, **kwargs)`


will be implemented in a same fashion as

`MultipleObjectMixin.get_context_data(self, *, object_list=None,
**kwargs)` is this moment.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:13>

Django

unread,
Aug 11, 2019, 1:27:42 PM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------
Description changed by Davit Gachechiladze:

Old description:

> Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


> like `context = self.get_context_data(object=self.object)`. Why should we
> pass `object=self.object` ? It's redundant (IMHO).
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
>
> def get_context_data(self, **kwargs):
> """Insert the single object into the context dict."""
> context = {}
> if self.object:
> context['object'] = self.object
> context_object_name =
> self.get_context_object_name(self.object)
> if context_object_name:
> context[context_object_name] = self.object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data(object=self.object)
> return self.render_to_response(context)
> }}}
>

> I think, it's better idea to implement

> `SingleObjectMixin.get_context_data(self, **kwargs)` and
> `BaseDetailView.get(self, request, *args, **kwargs)` like this. Code
> below
>

> {{{#!python
> class SingleObjectMixin(ContextMixin):
> def get_context_data(self, *, object=None, **kwargs):
> """Insert the single object into the context dict."""
> object = object if object is not None else self.object
> context = {}
> if object:
> context['object'] = object
> context_object_name = self.get_context_object_name(object)
> if context_object_name:
> context[context_object_name] = object
> context.update(kwargs)
> return super().get_context_data(**context)
> }}}
>
> {{{#!python
> class BaseDetailView(SingleObjectMixin, View):
> """A base view for displaying a single object."""
> def get(self, request, *args, **kwargs):
> self.object = self.get_object()
> context = self.get_context_data()
> return self.render_to_response(context)
> }}}
>

> Also, `SingleObjectMixin.get_context_data(self, *, object=None,
> **kwargs)` will be implemented in a same fashion as
> `MultipleObjectMixin.get_context_data(self, *, object_list=None,
> **kwargs)` is this moment.

New description:

Hi. In `BaseDetailView.get(self, request, *args, **kwargs)`, we have line


like `context = self.get_context_data(object=self.object)`. Why should we
pass `object=self.object` ? It's redundant (IMHO).

{{{#!python
class SingleObjectMixin(ContextMixin):

def get_context_data(self, **kwargs):
"""Insert the single object into the context dict."""
context = {}
if self.object:
context['object'] = self.object
context_object_name =
self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
}}}

I think, it's better idea to implement


`SingleObjectMixin.get_context_data(self, **kwargs)` and
`BaseDetailView.get(self, request, *args, **kwargs)` like this. Code below

{{{#!python


class SingleObjectMixin(ContextMixin):
def get_context_data(self, *, object=None, **kwargs):
"""Insert the single object into the context dict."""
object = object if object is not None else self.object
context = {}
if object:
context['object'] = object
context_object_name = self.get_context_object_name(object)
if context_object_name:
context[context_object_name] = object
context.update(kwargs)
return super().get_context_data(**context)
}}}

{{{#!python
class BaseDetailView(SingleObjectMixin, View):
"""A base view for displaying a single object."""
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data()
return self.render_to_response(context)
}}}

Also, `SingleObjectMixin.get_context_data(self, *, object=None, **kwargs)`


will be implemented in a same fashion as

`MultipleObjectMixin.get_context_data(self, *, object_list=None,
**kwargs)` is this moment.

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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:14>

Django

unread,
Aug 11, 2019, 2:23:40 PM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------

Comment (by felixxm):

Davit please don't update ticket description so many times, you can refer
PR in a comment.

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:15>

Django

unread,
Aug 11, 2019, 2:26:15 PM8/11/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: assigned
Component: Generic views | Version: master
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
-------------------------------------+-------------------------------------

Comment (by Davit Gachechiladze):

Replying to [comment:15 felixxm]:


> Davit please don't update ticket description so many times, you can
refer PR in a comment.

Thank you. I will try to do everything better in future.

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:16>

Django

unread,
Aug 12, 2019, 4:20:59 AM8/12/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: closed

Component: Generic views | Version: master
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:

Hi Davit,

Thanks for the suggestion. Strictly you're right, in theory this
simplification is available. We can't make it, however.

Firstly, they'll be plenty of folks that overrode `get_context_data()`
relying on the fact that `self.object` is injected.
(We change it, we break their code.)

Then, removing relies too much on internal knowledge of the CBV
implementations.

The general pattern for `get_context_data()` is this:

{{{
def get_context_data(self, **kwargs):
context = {
# Some stuff...
}

# Allow caller to inject the values we want...
context.update(kwargs)

return context
}}}

Without looking at `SingleObjectMixin` at all, the implementation of
BaseDetailView.get() is exactly what one want's: we want the calling
function to ''drive'' `get_context_data() by passing in the object to be
rendered.

I don't know if you've seen them but check out http://django-vanilla-
views.org/ for a stripped back take on GCBVs here.

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:17>

Django

unread,
Aug 12, 2019, 4:31:19 AM8/12/19
to django-...@googlegroups.com
#30698: `BaseDetailView` and `SingleObjectMixin` optimization.
-------------------------------------+-------------------------------------
Reporter: Davit Gachechiladze | Owner: Davit
Type: | Gachechiladze
Cleanup/optimization | Status: closed
Component: Generic views | Version: master
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:17 Carlton Gibson]:


> Hi Davit,
>
> Thanks for the suggestion. Strictly you're right, in theory this
simplification is available. We can't make it, however.
>
> Firstly, they'll be plenty of folks that overrode `get_context_data()`
relying on the fact that `self.object` is injected.
> (We change it, we break their code.)
>
> Then, removing relies too much on internal knowledge of the CBV
implementations.
>
> The general pattern for `get_context_data()` is this:
>
> {{{
> def get_context_data(self, **kwargs):
> context = {
> # Some stuff...
> }
>
> # Allow caller to inject the values we want...
> context.update(kwargs)
>
> return context
> }}}
>
> Without looking at `SingleObjectMixin` at all, the implementation of
BaseDetailView.get() is exactly what one want's: we want the calling
function to ''drive'' `get_context_data() by passing in the object to be
rendered.
>
> I don't know if you've seen them but check out http://django-vanilla-
views.org/ for a stripped back take on GCBVs here.

Thank you for explanation.

--
Ticket URL: <https://code.djangoproject.com/ticket/30698#comment:18>

Reply all
Reply to author
Forward
0 new messages