I created "Category" model with Django MPTT:
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
class Category(MPTTModel):
name = models.CharField(max_length=50)
parent = TreeForeignKey(
"self",
on_delete=models.CASCADE,
null=True,
blank=True,
related_name="children"
)
But, with this "Category" model, I could add the duplicated data "3F" and "4F" under "Building B"(Direct Parent) as shown below:
- USA
- New York
- Building A
- 3F
- 4F
- Building B
- 3F // Here
- 3F // Here
- 4F // Here
- 4F // Here
- Building A
- New York
So I added "unique=True" to "name" field in "Category" model:
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
class Category(MPTTModel): // Here
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey(
"self",
on_delete=models.CASCADE,
null=True,
blank=True,
related_name="children"
)
But with this "Category" model, I couldn't add the data "3F" and "4F" under "Building B" anymore because "3F" and "4F" already exist under "Building A" in "Category". I found adding "unique=True" to "name" field in "Category" model sets Unique Constraint in whole "Category" model about "name" field. So if there are the same "name" values such as "F3" and "F4" anywhere in "Category", we cannot add the same "name" values such as "F3" and "F4" anywhere in "Category". In short, if "3F" and "4F" already exist anywhere in "Category", we cannot add "3F" and "4F" anywhere in "Category":
- USA
- New York
- Building A
- 3F // Because already exists
- 4F // Because already exists
- Building B
- 3F // So cannot add
- 3F // So cannot add
- 4F // So cannot add
- 4F // So cannot add
- Building A
- New York
This is my desired result not allowing duplicated data only under the direct parents:
- USA
- New York
- Building A
- 3F // Even though already exists
- 4F // Even though already exists
- Building B
- 3F // 〇 But allowed to add
- 3F // ✖ Not allowed to add
- 4F // 〇 But allowed to add
- 4F // ✖ Not allowed to add
- Building A
- New York
Are there any ways to set Unique Constraint only under the direct parents?