This repository was archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
The VERSION_UNIQUE does not seem to work #162
Copy link
Copy link
Open
Description
I have the following
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels