django如何在iSeries期间对其他记录执行检查
我有以下Django型号:django如何在iSeries期间对其他记录执行检查,django,django-models,Django,Django Models,我有以下Django型号: class BarEvent(models.Model): EVENT_TYPES = ( ('op', 'opening'), ('cl', 'closing'), ('ea', 'event_a'), ('eb','event_b') ) event_type = models.CharField(max_length=2, choices=BAR_BALANCE_TYPES) date = models.DateField("Dat
class BarEvent(models.Model):
EVENT_TYPES = ( ('op', 'opening'), ('cl', 'closing'), ('ea', 'event_a'), ('eb','event_b') )
event_type = models.CharField(max_length=2, choices=BAR_BALANCE_TYPES)
date = models.DateField("Data", default=datetime.now)
其中BarEvent对象表示按日期和时间排序的事件。
我需要确保“开始”或“结束”事件交替发生(即,没有两个连续的“开始”或“结束”事件),因此,如果我尝试在另一个“开始”事件之后插入“开始”事件,则会阻止插入,但我不确定如何完成
我是否应该在重写的保存方法中对现有记录执行检查?您可以在模型中编写
clean
方法,以在实际保存对象之前检查额外的验证
class BarEvent(models.Model):
EVENT_TYPES = ( ('op', 'opening'), ('cl', 'closing'), ('ea', 'event_a'), ('eb','event_b') )
event_type = models.CharField(max_length=2, choices=BAR_BALANCE_TYPES)
date = models.DateField("Data", default=datetime.now)
def clean(self):
"""
Custom clean method to validate there can not be two
consecutive events of same type
"""
if self.objects.latest('date').event_type == self.event_type:
raise ValidationError('Consecutive events of same type %s' % self.event_type)
您可以在模型中编写
clean
方法,在实际保存对象之前检查额外的验证
class BarEvent(models.Model):
EVENT_TYPES = ( ('op', 'opening'), ('cl', 'closing'), ('ea', 'event_a'), ('eb','event_b') )
event_type = models.CharField(max_length=2, choices=BAR_BALANCE_TYPES)
date = models.DateField("Data", default=datetime.now)
def clean(self):
"""
Custom clean method to validate there can not be two
consecutive events of same type
"""
if self.objects.latest('date').event_type == self.event_type:
raise ValidationError('Consecutive events of same type %s' % self.event_type)
您提到的模型没有任何
结束日期
字段?每条记录都是一个单独的事件,有自己的日期时间,因此,表格可以是:“op'-01/01/2012 00:00'a1'-02/01/2012 00:00'cl'-03/01/2012 00:00'op'-03/01/2012 12:00”,但不能是:“op'-01/01/2012 00:00'a1'-02/01/2012 00:00'op'-03/01/2012 00:00”,因为有两个连续的“op”事件。您提到的模型没有任何<代码>结束日期<代码>字段?每个记录都是一个具有自己日期时间的单独事件,因此,表格可以是:“op'-01/01/2012 00:00”a1'-02/01/2012 00:00“cl'-03/01/2012 00:00”op'-03/01/2012 12:00,但不能是:“op'-01/01/2012 00:00”a1'-02/01/2012 00:00“op'-03/01/2012 00:00”,因为有两个连续的“op”events@user1924809这只是一个在插入期间如何对其他记录执行检查的示例。请添加您自己的验证逻辑。希望这能对你有所帮助。谢谢你的回复。我确实读过clean方法,但认为它只能用于单个记录中的字段验证,而不管现有记录值如何。我会试试的@user1924809这只是如何在插入期间对其他记录执行检查的示例。请添加您自己的验证逻辑。希望这能对你有所帮助。谢谢你的回复。我确实读过clean方法,但认为它只能用于单个记录中的字段验证,而不管现有记录值如何。我会试试的!