以下是我的例子:
我们有打印机。我们可以定义链接到特定打印机的页面格式,然后定义选择起始格式的工作流程(添加到打印作业的第一页),正文格式和结束格式(添加到打印作业的最后一页)。
不需要开始和结束(null和空白= True)
我想确保start,body和end将是同一台打印机的格式。
class Printer(models.Model):
name = models.CharField(max_length = 20)
class Format(models.Model):
name = models.CharField(max_length = 20)
format = models.TextField()
printer = models.ForeignKey(Printer)
class Workflow(models.Model):
name = models.CharField(max_length = 20)
startfmt = models.ForeignKey(Format, related_name = 'start_workflow', null = True, blank = True)
bodyfmt = models.ForeignKey(Format, related_name = 'start_workflow')
endfmt = models.ForeignKey(Format, related_name = 'start_workflow', null = True, blank = True)
所以我需要这个模型来验证共享同一台打印机的startfmt,bodyfmt和endfmt参考格式......怎么样?
答案 0 :(得分:1)
您最好的选择可能是工作流程模型中的overriding save:
class Workflow(models.Model):
# field definitions as you have them
def save(self, force_insert=False, force_update=False):
printer = self.bodyfmt.printer
if self.startfmt and self.startfmt.printer != printer:
raise ValueError("Startfmt printer does not match")
if self.endfmt and self.endfmt.printer != printer:
raise ValueError("Endfmt printer does not match")
super(Workflow, self).save(force_insert, force_update)