无法将上下文变量添加到 change_view 中,因为 extra_context 不是字典

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

我想在 ModelAdmin.change_view() 上添加一个字段来过滤我的内联对象。

基于this solution我尝试注入extra_context进去:

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)

不幸的是,方法变量 extra_context 是一个 unicode 字符串,django 引发:

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

正在调用 /admin/core/process/5/

是否可以将 object_id 插入到 extra_context 字典中以注入要过滤的表单?

ModelAdmin.change_view() 方法具有不同的签名:

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)