django - using form clean(self) method to do validation -
django - using form clean(self) method to do validation -
this forms.py
what wish accomplish is: before form gets saved- if there admin_time nowadays execution_time should replaced admin_time.
def clean(self): cleaned_data = super(taskform, self).clean() start_date = cleaned_data.get('start_date') end_date = cleaned_data.get('end_date') if start_date , end_date: if start_date >= end_date: raise forms.validationerror(_("'end date' must after 'start date'")) homecoming cleaned_data execution_time = self.cleaned_data['execution_time'] admin_time = self.cleaned_data['admin_time'] if admin_time: execution_time=admin_time cleaned_data.update(execution_time) cleaned_data.update(execution_time) homecoming cleaned_data
i'd write mixin views takes care of , can used in multiple places:
class foomixin(object): def form_valid(self, form): self.object = form.save(commit=false) if self.object.admin_time: self.object.execution_time = self.object.admin_time self.object.save() homecoming httpresponseredirect(self.get_success_url()) class mycreateview(foomixin, createview): pass class myupdateview(foomixin, updateview): pass
django forms
Comments
Post a Comment