0

How do I ensure that a unique combination of two properties should not be repeated

For instance in the following model

class modelBodyPart(models.Model):
    area = models.CharField(max_length=128)
    crush_name = models.CharField(max_length=128)

In each instance of modelBodyPart area and crush_name should always be different

for example some allowed and non-allowed results are:

  area = Area_A crush_name=Jenny //OK
  area = Area_A crush_name=Jordan //OK
  area = Area_B crush_name=Jenny //OK
  area = Area_A crush_name=Jenny //Not allowed

How would I implement this in the model ? Will I use unique_together I could not totally understand the case requirement from the above link that is why I am asking here.

Community
  • 1
  • 1
MistyD
  • 16,373
  • 40
  • 138
  • 240

1 Answers1

2

Yes you are right your code should be like this -

models.py

class modelBodyPart(models.Model):
    area = models.CharField(max_length=128)
    crush_name = models.CharField(max_length=128)

    class Meta:
         unique_together = ['area','crush_name']
subha.py
  • 463
  • 3
  • 18