930π
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',)
231π
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')
]
- [Django]-Django-social-auth django-registration and django-profiles β together
- [Django]-Django β Clean permission table
- [Django]-How to go from django image field to PIL image and back?
14π
in Django 4.0,
The new *expressions positional argument of UniqueConstraint() enables
creating functional unique constraints on expressions and database
functions. For example:
from django.db import models
from django.db.models import UniqueConstraint
from django.db.models.functions import Lower
class MyModel(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
class Meta:
constraints = [
UniqueConstraint(
Lower('first_name'),
Lower('last_name').desc(),
name='first_last_name_unique',
),
]
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-Django: how save bytes object to models.FileField?
- [Django]-Django β How to set default value for DecimalField in django 1.3?
7π
Yes you can define more than one field as unique using Django class Meta as this 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:
unique_together = ('volume_number', 'journal_id')
Note:
To make things go writes you should not add attribute unique=True
to any field that you define in unique_together
attribute otherwise it will not work as unique together.
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-Django urlsafe base64 decoding with decryption
- [Django]-Passing variable urlname to url tag in django template
4π
In Django 4.1.1
from django.db import models
class Volume(models.Model):
field_1 = models.CharField("field 1", max_length=100,)
field_2 = models.CharField("field 2", max_length=100,)
field_3 = models.CharField("field 3", max_length=100,)
class Meta:
unique_together = [['field_1', 'field_2']]
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
- [Django]-Django return file over HttpResponse β file is not served correctly
- [Django]-Using Cloudfront with Django S3Boto