数据模型:
代码语言:javascript复制class Task(models.Model):
taskStartTime = models.DateTimeField(verbose_name='开始时间')
taskEndTime = models.DateTimeField(verbose_name='结束时间')
def __str__(self):
return self.taskName
添加约束:
代码语言:javascript复制class Task(models.Model):
taskStartTime = models.DateTimeField(verbose_name='开始时间')
taskEndTime = models.DateTimeField(verbose_name='结束时间')
class Meta:
verbose_name = '任务管理'
verbose_name_plural = verbose_name
constraints = [
models.CheckConstraint(
check=Q(taskStartTime__lte=F('taskEndTime')), name='datetimecheck'
),
]
def __str__(self):
return self.taskName