views:

299

answers:

1

Is there a way to specify a Model in Django such that is ensures that pair of fields in unique in the table, in a way similar to the "unique=True" attribute for similar field?

Or do I need to check this constraint in the clean() method?

+12  A: 

There is a META option called unique_together. For example:

class MyModel(models.Model):
    field1 = models.BlahField()
    field2 = models.FooField()
    field3 = models.BazField()

    class Meta:
        unique_together = ("field1", "field2")

More info on the Django documentation page.

Baishampayan Ghose
META should not be in all caps; it should be "class Meta:"
Carl Meyer
Carl: Thanks for the tip. That was a typo.
Baishampayan Ghose