Opened 5 years ago

Closed 5 years ago

Last modified 5 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: master
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 5 years ago.
Documentation fix

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by Claude Paroz

Attachment: 17749.diff added

Documentation fix

comment:1 Changed 5 years ago by Claude Paroz

Has patch: set
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted
Version: 1.3SVN

comment:2 Changed 5 years ago by Tim Graham

Resolution: fixed
Status: newclosed

In [17582]:

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

comment:3 Changed 5 years ago by Tim Graham

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