在我的django項目中,我有2個相關模型「Vehicle」(父模型)和ParamConf(子模型)。 ParamConf有字段「程序」,我想修復一些錯誤的值。
(admin.py):如何更改django管理員中的內嵌表單?
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()方法,但Django不希望保存車輛的形式(車輛的保存方法是好的)之後調用它。