例:
文章点赞
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字段必须唯一,否则报错。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:django中models联合唯一unique_together - Python技术站