1

我创建了以下模型:

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) 无关紧要。

更新 我根据文档创建了自定义验证器

表格.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弄清楚“注册”语句有问题。它应该是:

admin.site.register(RentPeriod, RentPeriodAdmin)
4

1 回答 1

0

我建议模型上的 validate_unique 不是这样做的地方。

要进行此级别的验证,您需要模型有一个 PK,但是为了有一个 PK,它需要保存到 DB,但是为了保存到 DB,它需要验证。看到问题了吗?

您需要将这种验证移动到表单/表单集中。这意味着重载 InlineFormset 的 clean() 方法,我假设您使用该方法输入小时数。参考:Django 中的内联表单验证

于 2013-04-02T06:13:41.530 回答