gpt4 book ai didi

python - Django 条件唯一在一起

转载 作者:太空狗 更新时间:2023-10-29 22:08:21 32 4
gpt4 key购买 nike

我有一个如下所示的模型:

class LibraryEntry(models.Model):
host_lib_song_id = models.IntegerField()
song = models.CharField(max_length=200)
artist = models.CharField(max_length=200)
album = models.CharField(max_length=200)
owning_user = models.ForeignKey(User)
is_deleted = models.BooleanField(default=False)

现在,如果我选择 where is_deleted=Falsehost_lib_song_idowning_user 的组合应该是唯一的。我该如何表达呢?

最佳答案

覆盖 validate_unique如果 is_deletedFalse 则检查唯一性更合适:

...

def validate_unique(self, exclude=None):
if not self.is_deleted and \
LibraryEntry.objects.exclude(pk=self.pk).filter(host_lib_song_id=self.host_lib_song_id, owning_user=self.owning_user).exists():
raise ValidationError('Some error message about uniqueness required')
super(LibraryEntry, self).validate_unique(exclude=exclude)

关于python - Django 条件唯一在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245430/

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