【问题标题】:Adding context variable into change_view not possible because extra_context not a dictionary无法将上下文变量添加到 change_view 中,因为 extra_context 不是字典
【发布时间】:2015-05-07 11:26:34
【问题描述】:

我想在 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字典注入表单进行过滤?

【问题讨论】:

    标签: django django-admin django-1.7


    【解决方案1】:

    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)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    相关资源
    最近更新 更多