#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: > {{{#!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. > > [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 <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.bf8bc404947937158f2fa31bb010f70a%40djangoproject.com.