2012-07-18 86 views
0

在我的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不希望保存車輛的形式(車輛的保存方法是好的)之後調用它。

回答

0

如果字段值實際發生更改(並且我試圖保存沒有編輯字段的表單),Django會爲內聯參數發出保存信號。對不起,這是我的錯。

0

我知道這個問題已經很老了,但別人可能還在尋找答案...保存,即使它沒有改變一個內聯:

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