5👍
✅
Unique together constraint is applied at the database level. Many databases do not compare null
values each other and hence, let the insert operations to go in.
You can fix it by overriding clean
method in your model. clean
method should be used to provide custom validation or to modify field values before saving. Also, note cleanis not invoked when you call
saveon the object. It should be invoked before calling the
save` method.
from django.core.exceptions import ValidationError
class Record(Model):
def clean(self):
# check if exists
if Record.objects.get(type=self.type,
code=self.code,
group=self.group):
# raise an exception
raise ValidationError("Exists")
- [Django]-Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x
0👍
1)
try:
//somthing
except IntegrityError as e:
print("integrity")
except Exception as e:
print(e)`
2)Check it
record=Record(type=type_article_structure,
code='shoe',
group=None)
record.save()
- [Django]-Django .latest() values
- [Django]-Error using Django-Hstore for PostgreSQL HSTORE in Django
- [Django]-Sending emails after updating the model from the admin page (Django)
- [Django]-Django Rest Framework: DRYer pagination for custom actions
Source:stackexchange.com