1👍
✅
You work with a UniqueConstraint
[Django-doc]:
class Child(models.Model):
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
def __str__(self):
return f'{self.name}'
class Meta:
constraints = [
models.UniqueConstraint(
fields=('name', 'parent_id'), name='unique_child_name_per_parent'
)
]
Source:stackexchange.com