我正在尝试编写一个表单清理方法,以确保输入的一对日期当前不被存储在模型中的日期范围所覆盖。
所以我想捕捉fromdate >= value <= todate
,但between
我的干净方法中的过滤器没有得到它。
任何人都可以提供一些启示吗?
模型.py
class Dates(models.Model):
fromdate = models.DateField()
todate = models.DateField()
表格.py
class Form(forms.Form):
fromdate = forms.DateField()
todate = forms.DateField()
class Meta:
model = Dates
def clean_fromdate(self):
form_fromdate = self.cleaned_data.get('fromdate')
exists = Dates.objects.filter(fromdate__contains=form_fromdate).exists()
if exists == True:
raise forms.ValidationError(_("A period is already entered for this date"))
between = Dates.objects.filter(fromdate__gte=form_fromdate,
todate__lte=form_fromdate).exists()
if between == True:
raise forms.ValidationError(_("Period already between this date 1"))
return form_fromdate
def clean_todate(self):
form_todate = self.cleaned_data.get('todate')
exists = Dates.objects.filter(todate__contains=form_todate).exists()
if exists == True:
raise forms.ValidationError(_("A period is already entered for this date"))
between = PeriodsOfInactivity2012.objects.filter(fromdate__gte=form_todate,
todate__lte=form_todate).exists()
if between == True:
raise forms.ValidationError(_("Period already between this date 2"))
return form_todate