如何将模型外键限制为具有相同属性的外来对象
这是我的例子:
我们有打印机。我们可以定义链接到特定打印机的页面格式,然后定义选择起始格式(添加到打印作业的第一页)、正文格式和结束格式(添加到打印作业的最后一页)的工作流程。
不需要开始和结束(空和空白 = True)
我想确保开始、正文和结束将是同一打印机的格式。
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 参考格式...如何?
Here is my example :
We have printers. We can define page formats that are linked to a specific printer then we define workflows that select a starting format (first page added to the printing job), a body format and an end format (last page added to the printing job).
Start and End are not required (null and blank = True)
I want to be sure that start, body and end will be formats of the same printer.
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)
So I need this model to validate that startfmt, bodyfmt and endfmt reference formats that share the same printer... how ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你最好的选择可能是覆盖保存 在工作流模型中:
Your best bet is probably overriding save in the Workflow model: