[Answered ]-Django: UniqueConstraint validator does work neither for model nor for intermediate model

1👍

You should add these to a variable named constraints in the Meta options, so:

class RecipeTag(models.Model):
    # ⋮

    class Meta:
        verbose_name = 'отношение рецепта к тэгу'
        verbose_name_plural = 'отношение рецепта к тэгам'

        #   ↓ add this to a variable named constraints
        constraints = [
            models.UniqueConstraint(
                fields=('recipe_id', 'tag_id'),
                name='recipe_tag_unique_constraint'
            )
        ]

The constraint can only span over the table of the RecipeTag, so recipe__id and tag__id do not make much sense, you can work with recipe_id and tag_id.

Leave a comment