Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#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)

17749.diff (775 bytes ) - added by Claude Paroz 13 years ago.
Documentation fix

Download all attachments as: .zip

Change History (4)

by Claude Paroz, 13 years ago

Attachment: 17749.diff added

Documentation fix

comment:1 by Claude Paroz, 13 years ago

Has patch: set
Triage Stage: UnreviewedAccepted
Version: 1.3SVN

comment:2 by Tim Graham, 13 years ago

Resolution: fixed
Status: newclosed

In [17582]:

Fixed #17749 - Documented better way of overriding ModelAdmin; thanks chrisdpratt and claudep.

comment:3 by Tim Graham, 13 years ago

In [17583]:

[1.3.X] Fixed #17749 - Documented better way of overriding ModelAdmin; thanks chrisdpratt and claudep.

Backport of r17582 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top