Django: How to override form.save ()?

My model has quite a few logical fields. I split them into 3 sets, which I show as MultipleChoiceField with the modified CheckboxSelectMultiple .

Now I need to save this data back to the database. that is, I need to split the data returned by one widget into several Boolean columns. I think this is suitable for the save() method, no?

Question: How do I do this? Something like that?

 def save(self, commit=True): # code here return super(MyForm, self).save(commit) 

If so ... how to set values?

 self.fields['my_field'].value = 'my_flag' in self.cleaned_data['multi_choice'] 

Or something? Where is all the data stored?

+45
django django-forms
Oct 13 '10 at 19:33
source share
1 answer

The place where you want to save your data is your new instance of the model:

 def save(self, commit=True): instance = super(MyForm, self).save(commit=False) instance.flag1 = 'flag1' in self.cleaned_data['multi_choice'] # etc if commit: instance.save() return instance 
+75
Oct. 14 '10 at 2:37
source share



All Articles