Это мои модели:
class Post(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(fields=['user'], condition=Q(is_featured=True), name='unique featured post per user'),
]
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
post = models.TextField()
is_featured = models.BooleanField(default=False)
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
comment = models.CharField(max_length=500)
class Like(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(fields=['user', 'comment'], name='unique like per comment'),
]
user = models.ForeignKey(User, on_delete=models.CASCADE)
comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
is_positive = models.BooleanField(blank=True, null=True)
ЦЕЛЬ:
Я хотел бы добавить ограничение, когда user
не может понравиться его собственный комментарий.
Я пытался, но, конечно, я не могу сделать это:
models.UniqueConstraint(fields=['user'], condition=~Q('user__id__in'=Like.objects.values_list('user_id', flat=True), name='unique like per comment'),