Django 做联合唯一操作 unique_together

文章点赞

class ArticleUpDown(models.Model):
    """
    点赞表
    """
    nid = models.AutoField(primary_key=True)
    user = models.ForeignKey(to="UserInfo", null=True, on_delete=models.CASCADE)
    article = models.ForeignKey(to="Article", null=True, on_delete=models.CASCADE)
    is_up = models.BooleanField(default=True)

    class Meta:
        unique_together = (("article", "user"),)
        verbose_name="点赞"
        verbose_name_plural=verbose_name

意思为在这个表中,每一行的user、artical字段必须唯一,否则报错。

你可能感兴趣的:(ORM)