#17749 closed Cleanup/optimization (fixed)
Admin documentation on views suggests poor way of adding extra_context
| Reported by: | Chris Pratt | Owned by: | nobody |
|---|---|---|---|
| Component: | Documentation | Version: | dev |
| Severity: | Normal | Keywords: | admin, documentation, docs |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
As reference see: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
If you pass my_context for extra_context in this way, it completely drops any existing context passed in from a subclass also overriding this method. This could lead to a confusing scenario in which Django is seemingly ignoring extra_context. One better approach would be:
def change_view(self, request, object_id, extra_context=None):
extra_context = extra_context or {}
extra_context['osm_data'] = self.get_osm_info()
return super(MyModelAdmin, self).change_view(request, object_id,
extra_context=extra_context)
It's a relatively small difference, I'll admit, but since the docs are probably most frequently read by those new to Django and possibly even Python itself, I believe the sample code should take into account eventualities such as the ModelAdmin being subclassed.
Attachments (1)
Change History (4)
by , 14 years ago
| Attachment: | 17749.diff added |
|---|
comment:1 by , 14 years ago
| Has patch: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Version: | 1.3 → SVN |
Documentation fix