在我的 django 项目中,我有 2 个相关模型“Vehicle”(父模型)和 ParamConf(子模型)。ParamConf 有“程序”字段,我想修复其中的一些错误值。
(管理员.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 表单后调用它(Vehicle 的 save 方法还可以)。