Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 520
GregorVolkmann Avatar asked Jun 22 '26 18:06

GregorVolkmann


1 Answers

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)
like image 145
catavaran Avatar answered Jun 25 '26 13:06

catavaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!