Django 联合唯一UniqueConstraint
from django.db import models class UserAttention(models.Model): watcher = models.ForeignKey('user.User', on_delete=models.CASCADE, related_name="attention") goal = models.ForeignKey('goal.Goal', on_delete=models.CASCADE, related_name="watcher") level = models.IntegerField(verbose_name="亲密度", default=0) class Meta: constraints = [models.UniqueConstraint(fields=['watcher', 'goal'], name='unique_attention')]
用来限制watcher和goal联合唯一索引