The Python Oracle

How to specify uniqueness for a tuple of field in a Django model

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Future Grid Looping

--

Chapters
00:00 How To Specify Uniqueness For A Tuple Of Field In A Django Model
00:22 Accepted Answer Score 42
00:39 Thank you

--

Full question
https://stackoverflow.com/questions/4723...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #djangomodels

#avk47



ACCEPTED ANSWER

Score 42


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.