Opened 5 years ago
Last modified 5 years ago
#30698 closed Cleanup/optimization
`BaseDetailView` and `SingleObjectMixin` optimization. — at Version 5
Reported by: | Davit Gachechiladze | Owned by: | Davit Gachechiladze |
---|---|---|---|
Component: | Generic views | Version: | dev |
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 )
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).
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)
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
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)
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.
Change History (5)
comment:1 by , 5 years ago
Description: | modified (diff) |
---|---|
Has patch: | set |
comment:2 by , 5 years ago
Description: | modified (diff) |
---|
comment:3 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 5 years ago
Description: | modified (diff) |
---|
comment:5 by , 5 years ago
Description: | modified (diff) |
---|
Note:
See TracTickets
for help on using tickets.