如何将模型外键限制为具有相同属性的外来对象

发布于 2024-08-14 17:21:30 字数 831 浏览 3 评论 0原文

这是我的例子:

我们有打印机。我们可以定义链接到特定打印机的页面格式,然后定义选择起始格式(添加到打印作业的第一页)、正文格式和结束格式(添加到打印作业的最后一页)的工作流程。

不需要开始和结束(空和空白 = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-08-21 17:21:30

你最好的选择可能是覆盖保存 在工作流模型中:

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)

Your best bet is probably overriding save in the Workflow model:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文