Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define two fields "unique" as couple

Is there a way to define a couple of fields as unique in Django?

I have a table of volumes (of journals) and I don't want more then one volume number for the same journal.

class Volume(models.Model):     id = models.AutoField(primary_key=True)     journal_id = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Journal")     volume_number = models.CharField('Volume Number', max_length=100)     comments = models.TextField('Comments', max_length=4000, blank=True) 

I tried to put unique = True as attribute in the fields journal_id and volume_number but it doesn't work.

like image 814
Giovanni Di Milia Avatar asked Feb 04 '10 17:02

Giovanni Di Milia


People also ask

What is unique together?

We want the user and the title to be unique together. In other words, a user cannot create two posts with the same title. Therefore, we make the user and the title fields unique together. To make fields unique together, we create a class Meta.

How do you make two fields unique in Django?

Using the constraints features UniqueConstraint is preferred over unique_together. From the Django documentation for unique_together : Use UniqueConstraint with the constraints option instead. UniqueConstraint provides more functionality than unique_together.

What is Django Unique_together?

unique_together may be deprecated in the future. This is a list of lists that must be unique when considered together. It's used in the Django admin and is enforced at the database level (i.e., the appropriate UNIQUE statements are included in the CREATE TABLE statement).

What does class Meta do in Django?

Model Meta is basically the inner class of your model class. Model Meta is basically used to change the behavior of your model fields like changing order options,verbose_name, and a lot of other options. It's completely optional to add a Meta class to your model.


2 Answers

There is a simple solution for you called unique_together which does exactly what you want.

For example:

class MyModel(models.Model):   field1 = models.CharField(max_length=50)   field2 = models.CharField(max_length=50)    class Meta:     unique_together = ('field1', 'field2',) 

And in your case:

class Volume(models.Model):   id = models.AutoField(primary_key=True)   journal_id = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Journal")   volume_number = models.CharField('Volume Number', max_length=100)   comments = models.TextField('Comments', max_length=4000, blank=True)    class Meta:     unique_together = ('journal_id', 'volume_number',) 
like image 109
Jens Avatar answered Sep 26 '22 06:09

Jens


Django 2.2+

Using the constraints features UniqueConstraint is preferred over unique_together.

From the Django documentation for unique_together:

Use UniqueConstraint with the constraints option instead.
UniqueConstraint provides more functionality than unique_together.
unique_together may be deprecated in the future.

For example:

class Volume(models.Model):     id = models.AutoField(primary_key=True)     journal_id = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name="Journal")     volume_number = models.CharField('Volume Number', max_length=100)     comments = models.TextField('Comments', max_length=4000, blank=True)      class Meta:         constraints = [             models.UniqueConstraint(fields=['journal_id', 'volume_number'], name='name of constraint')         ] 
like image 38
daaawx Avatar answered Sep 24 '22 06:09

daaawx