【问题标题】:Django validating time slotDjango 验证时隙
【发布时间】:2021-08-31 00:01:04
【问题描述】:

这是我的模型,用于存储新预订时特定时间的可用性

class TimeSlot(models.Model):
day = models.ForeignKey(
    Day,
    on_delete=models.CASCADE,
    related_name="time"
)
booking = models.ForeignKey(
    Booking,
    on_delete=models.CASCADE,
    related_name="time"
)
start_hour = models.TimeField()
end_hour = models.TimeField()

class Meta:
    unique_together = [('end_hour', 'start_hour',)]


def clean(self):
    pass

目前它允许预订,即使那些在 end_hour 和 start_hour 方面被认为是重复的。我想阻止空档,这样就不会在已预订的范围之间放置新的预订。

谁能知道如何使用范围?

【问题讨论】:

    标签: django django-models django-orm django-postgresql


    【解决方案1】:

    我认为问题在于允许添加已经存在的时间范围内的start_hour 和end_hour。当然 unique_together 约束不能处理这个问题,因为它只处理唯一性而不是一个范围内的唯一性。相反,您可以覆盖您的模型 clean 方法并在那里执行此验证:

    from django.db.models import Q
    from django.core.exceptions import ValidationError
    
    
    class TimeSlot(models.Model):
        day = models.ForeignKey(
            Day,
            on_delete=models.CASCADE,
            related_name="time"
        )
        booking = models.ForeignKey(
            Booking,
            on_delete=models.CASCADE,
            related_name="time"
        )
        start_hour = models.TimeField()
        end_hour = models.TimeField()
        
        class Meta:
            unique_together = [('end_hour', 'start_hour',)]
        
        
        def clean(self):
            start_hour_in_range = Q(start_hour__lte=self.start_hour, end_hour__gte=self.start_hour)
            end_hour_in_range = Q(start_hour__lte=self.end_hour, end_hour__gte=self.end_hour)
            # Queryset that finds all clashing timeslots with the same day
            queryset = self._meta.default_manager.filter(start_hour_in_range | end_hour_in_range, day=self.day)
            if self.pk:
                queryset = queryset.exclude(pk=self.pk) # Exclude this object if it is already saved to the database
            if queryset.exists():
                raise ValidationError('An existing timeslot clashes with the given one!')
    

    接下来,如果您使用ModelForm,此方法将被自动调用,否则您可以调用instance.full_clean(),它将调用此方法和模型上的所有其他清洁方法(clean_fields 和validate_unique )。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      相关资源
      最近更新 更多