如何在django admin中更改内联表单?

时间:2012-07-18 09:09:16

标签: python django

在我的django项目中,我有2个相关模型“Vehicle”(父模型)和ParamConf(子模型)。 ParamConf有字段“程序”,我想在其中修正一些错误的值 (admin.py):

class ParamConfFormSet(BaseInlineFormSet):
    def clean(self):
        super(ParamConfFormSet, self).clean()

        for form in self.forms:
            if hasattr(form, 'cleaned_data') and 'program' in form.cleaned_data:

                program = form.cleaned_data['program'].lower() # <<< I want to save this changed value
                form.cleaned_data['program'] = program # <<< but this doesn't work :^(

class ParamConfInline(admin.TabularInline):
    model = models.ParamConf
    formset = ParamConfFormSet


class VehicleAdminForm(forms.ModelForm):
    class Meta:
        model = models.Vehicle

class VehicleAdmin(admin.ModelAdmin):
    inlines = [ ParamConfInline, ]
    form = VehicleAdminForm

我甚至为ParamConf编写了save()方法,但django在保存Vehicle表格后不想调用它(车辆的保存方法没问题)。

2 个答案:

答案 0 :(得分:0)

如果字段值实际发生了变化,Django会为内联参数发出保存信号(我试图在不编辑字段的情况下保存表单)。对不起,这是我的错。

答案 1 :(得分:0)

我知道这个问题已经过时了,但其他人可能仍在寻找答案......即使没有更改,也要保存内联:

Class EntityInline(admin.TabularInline):
    extra = 0
    model = Entity
    form = AlwaysChangedModelForm