Code

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#17749 closed Cleanup/optimization (fixed)

Admin documentation on views suggests poor way of adding extra_context

Reported by: chrisdpratt 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 claudep 2 years ago.
Documentation fix

Download all attachments as: .zip

Change History (4)

Changed 2 years ago by claudep

Documentation fix

comment:1 Changed 2 years ago by claudep

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.3 to SVN

comment:2 Changed 2 years ago by timo

  • Resolution set to fixed
  • Status changed from new to closed

In [17582]:

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

comment:3 Changed 2 years ago by timo

In [17583]:

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

Backport of r17582 from trunk.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.