class ChemLabTimeSelect(forms.Form):
from_date = forms.DateTimeField(
widget=DateTimePicker(options={"format": "YYYY-MM-DD HH:mm:ss",
"pickTime": True}))
to_date = forms.DateTimeField(
widget=DateTimePicker(options={"format": "YYYY-MM-DD HH:mm:ss",
"pickTime": True}))
def clean(self):
cleaned_data = self.cleaned_data
if cleaned_data.has_key('from_date') and cleaned_data.has_key('to_date'):
total_days = (cleaned_data['to_date'] - cleaned_data['from_date']).days
negative_days = (cleaned_data['from_date'] - cleaned_data['to_date']).total_seconds()
if negative_days > 0:
raise ValidationError({'from_date':["The from date is greater than the to date!",]})
elif total_days > 7:
raise ValidationError(["The amount of time selected is %s days which is too large!" % total_days,])
return cleaned_data
您是否研究过重写窗体的clean()函数?在
举个例子,我过去做过一些事情:
此外,该模型有一个clean()重写函数,应该能够达到您想要的结果。但是我们应该意识到表单和模型的干净覆盖之间的区别。在
相关问题 更多 >
编程相关推荐