在我的Django(1.2)项目中,我想以模型形式预填充字段,但是新值将被忽略。这是代码段:
class ArtefactForm(ModelForm): material = CharField(widget=AutoCompleteWidget('material', force_selection=False)) def __init__(self, *args, **kwargs): super(ArtefactForm, self).__init__(*args, **kwargs) self.fields['material'].initial = 'Test'
我也尝试了使用self.base_fields,但没有效果:总是在表单中显示数据库值。有任何想法吗?
self.base_fields
尝试这个:
def __init__(self, *args, **kwargs): initial = kwargs.get('initial', {}) initial['material'] = 'Test' kwargs['initial'] = initial super(ArtefactForm, self).__init__(*args, **kwargs)