Skip to content
This repository was archived by the owner on Feb 7, 2019. It is now read-only.
This repository was archived by the owner on Feb 7, 2019. It is now read-only.

The VERSION_UNIQUE does not seem to work #162

Description

@simkimsia

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions