You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 7, 2019. It is now read-only.
class VersionedWBSElement(Versionable):
# wbs number is supposed to be unique
wbs_number = models.CharField(max_length=100)
wbs_description = models.TextField()
VERSION_UNIQUE = [['wbs_number']]
I can still create duplicate with the same wbs_number despite the VERSION_UNIQUE
My workaround is install django-partial-index as a package
and then
from partial_index import PQ, PartialIndex, ValidatePartialUniqueMixin
class VersionedWBSElement(Versionable, ValidatePartialUniqueMixin):
# wbs number is supposed to be unique
wbs_number = models.CharField(max_length=100)
wbs_description = models.TextField()
class Meta:
indexes = [
PartialIndex(fields=['wbs_number'], unique=True, where=PQ(version_end_date__isnull=True))
]
And because I use DRF, my serializers.py looks like this:
from django.core.exceptions import ValidationError
from rest_framework import serializers
from dynamic_rest.serializers import DynamicModelSerializer
class VersionedWBSElementSerializer(DynamicModelSerializer):
wbs_number = serializers.CharField()
def validate_wbs_number(self, wbs_number):
try:
VersionedWBSElement(wbs_number=wbs_number).validate_unique()
## I added try-except in order to overwrite the error message
except ValidationError as e:
raise serializers.ValidationError("This WBS number already exists.")
return wbs_number