Want to define two fields unique as couple in Python? There is a simple solution for you called unique_together which does exactly what you want.
Example:
1 2 3 4 5 6 | 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:
1 2 3 4 5 6 7 8 | 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',) |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.