我创建了以下模型:
class BasePrice(models.Model):
start_hour = models.TimeField(blank=False)
end_hour = models.TimeField(blank=False)
monday = models.BooleanField(blank=False)
tuesday = models.BooleanField(blank=False)
wednesday = models.BooleanField(blank=False)
thursday = models.BooleanField(blank=False)
friday = models.BooleanField(blank=False)
price = models.IntegerField(blank=False)
def __unicode__(self):
days = ""
if (self.monday == True):
days = days + " Mon"
if (self.tuesday == True):
days = days + " Tue"
if (self.wednesday == True):
days = days + " Wed"
if (self.thursday == True):
days = days + " Thu"
if (self.friday == True):
days = days + " Fri"
return "Price " + str(self.price) + " at " + str(self.start_hour) + ":" +str(self.end_hour) + " in " + days
def clean(self):
if self.start_hour > self.end_hour:
raise ValidationError('Start hour is older than end hour!')
class RentPeriod(models.Model):
start_date = models.DateField(blank=False)
end_date = models.DateField(blank=False)
desk = models.ForeignKey(Desk)
base_prices = models.ManyToManyField(BasePrice)
def __unicode__(self):
return "Period " + str(self.start_date) + " to " + str(self.end_date) + " for " + self.desk.__unicode__()
def clean(self):
if self.start_date > self.end_date:
raise ValidationError('Start date is older than end date!')
def validate_unique(self, *args, **kwargs):
super(RentPeriod, self).validate_unique(*args, **kwargs)
# overlaping hours
basePrices = self.base_prices.all()
for price in basePrices:
qs = self.__class__._default_manager.filter(
(Q(monday=True) & Q(monday=price.monday)) |
(Q(tuesday=True) & Q(tuesday=price.tuesday)) |
(Q(wednesday=True) & Q(wednesday=price.wednesday)) |
(Q(thursday=True) & Q(thursday=price.thursday)) |
(Q(friday=True) & Q(friday=price.friday)),
start_hour__lte=self.end_hour,
end_hour__gte=self.start_hour
)
if qs.exists():
raise ValidationError({NON_FIELD_ERRORS: ('overlaping hours range',)})
一般情况下,我希望通过管理站点添加RentPeriod实例时避免在指定日期重叠时间。当我尝试添加RentPeriod实例时,出现以下错误:
在可以使用多对多关系之前,''需要具有字段“rentperiod”的值。
我已阅读click,但我不知道如何让它发挥作用。你能帮忙吗?
注意:复杂性(n ^ 2)在这种情况下无关紧要。
更新 我已根据documentation
创建了自定义验证程序forms.py
from biurrko.rents.models import RentPeriod
from django.forms.models import ModelForm
class RentPeriodForm(ModelForm):
class Meta:
model = RentPeriod
def clean(self):
cleaned_data = self.cleaned_data
basePrices = cleaned_data['base_prices']
end_hour = cleaned_data['end_hour']
start_hour = cleaned_data['start_hour']
for price in basePrices:
qs = basePrices.filter(
(Q(monday=True) & Q(monday=price.monday)) |
(Q(tuesday=True) & Q(tuesday=price.tuesday)) |
(Q(wednesday=True) & Q(wednesday=price.wednesday)) |
(Q(thursday=True) & Q(thursday=price.thursday)) |
(Q(friday=True) & Q(friday=price.friday)),
start_hour__lte=end_hour,
end_hour__gte=start_hour
)
if qs.exists():
raise ValidationError({NON_FIELD_ERRORS: ('overlaping hours range',)})
# only for test
raise ValidationError({NON_FIELD_ERRORS: ('reached',)})
# return cleaned_data
和admin.py
from django.contrib import admin
from biurrko.rents.models import Desk, Room, RentPeriod, BasePrice
from biurrko.rents.forms import RentPeriodForm
admin.site.register(Desk)
admin.site.register(Room)
admin.site.register(RentPeriod)
admin.site.register(BasePrice)
class RentPeriodAdmin(admin.ModelAdmin):
form = RentPeriodForm
不幸的是,自己的验证器“未被调用” - 我的意思是即使测试ValidationError也没有提升。
更新2 找出“register”语句出现问题。它应该是:
admin.site.register(RentPeriod, RentPeriodAdmin)
答案 0 :(得分:0)
我建议模型上的validate_unique不是这样做的地方。
要进行此级别的验证,您需要模型具有PK,但为了获得PK,需要将其保存到DB,但为了保存到DB,它需要验证。看到问题了?
您需要将此类验证移动到表单/ formset中。这意味着重载InlineFormset的clean()方法,我假设您使用它来输入小时数。参考:Inline Form Validation in Django