gpt4 book ai didi

python - Django - 外键约束的独特集合

转载 作者:行者123 更新时间:2023-12-04 13:51:59 28 4
gpt4 key购买 nike

我有两个这样的模型:

class Group(...):
pass

class Identifier(...):
value = models.CharField(...)
group = models.ForeignKey('Group', ..., related_named = 'identifiers')
我怎样才能:
  • 限制 Group 最多只有 4 个 Identifiers
  • 确保最多 4 个 Identifiers(标识符的值)的任何组合在所有 Groups 中都是唯一的?

  • 对于第 2 部分,这里是一个扁平化 Groups 表的示例:
    row | id__0__val | id__1__val | id__2__val | id__3__val 
    --- | ---------- | ---------- | ---------- | ----------
    0 | abc | 123 | xyz | 456
    1 | abc | 123 | xyz | - <-- valid (nulls are okay)
    2 | 123 | abc | xyz | 456 <-- invalid (same combo as row 0)
    以前我尝试过(类似的)这个,但它看起来很乱,功能有限,我不确定它是否会起作用:
    class Group(...):
    id__0 = models.OneToOneField('Identifier', blank = True, null = True, ...)
    id__1 = models.OneToOneField('Identifier', blank = True, null = True, ...)
    id__2 = models.OneToOneField('Identifier', blank = True, null = True, ...)
    id__3 = models.OneToOneField('Identifier', blank = True, null = True, ...)

    class Meta:
    unique_together = ('id__0__value', 'id__1__value', 'id__2__value', 'id__3__value')
    处理这种约束的更好方法是什么?

    最佳答案

    可以通过 validate_unique 来做方法:

    class Group(models.Model):
    ...

    def validate_unique(self, exclude=None):
    qs = Identifier.objects.filter(group_id=self.id)
    # restrict a Group to only have at most 4 Identifiers
    if qs.count() >= 4:
    raise ValidationError("group already has 4 Identifiers")
    # Ensure that any combination of up to 4 Identifiers is unique across all Groups
    values = []
    for group in Group.objects.all():
    values.append([i.value for i in group.identifiers])
    current_values = [i.value for i in self.identifiers]
    # https://stackoverflow.com/questions/22483730/python-check-if-list-of-lists-of-lists-contains-a-specific-list
    if current_values in values:
    raise ValidationError("group with these identifiers already exists")


    这是伪代码,所以可能不起作用 - 但你明白了:)

    关于python - Django - 外键约束的独特集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68492172/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com