Search code examples
djangodjango-admindjango-1.7

Adding context variable into change_view not possible because extra_context not a dictionary


I would like to add a field on the ModelAdmin.change_view() to filter my inline objects.

Based on this solution I tried to inject extra_context into it:

class ProcessAdmin(admin.ModelAdmin):
    inlines = [StepInline,]
    exclude = ('steps',)
    prepopulated_fields = {'name_slug': ('name',)}

    def change_view(self, request, extra_context=None):
        print(extra_context)
        extra = extra_context or {}
        extra['filter_form'] = FilterForm()
        return super(ProcessAdmin, self).change_view(request, extra_context=extra)

Unfortunately, the method variable extra_context is a unicode string and django raises:

TypeError, Exception Value: 'unicode' object does not support item assignment

on calling /admin/core/process/5/.

Is it possible to insert the object_id into the extra_context dictionary to inject a form to filter?


Solution

  • The ModelAdmin.change_view() method has a different signature:

    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra = extra_context or {}
        extra['filter_form'] = FilterForm()
        return super(ProcessAdmin, self).change_view(request, object_id,
                                                     form_url, extra_context=extra)