Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a validation mixin for DRF Serializers (can elaborate on this) #13

Open
simkimsia opened this issue Oct 9, 2018 · 0 comments
Open

Comments

@simkimsia
Copy link

simkimsia commented Oct 9, 2018

Hi there, can you elaborate more on this future plan?

Add a validation mixin for DRF Serializers

By the way, this is a great plugin. Thanks. Adding it to my project now.

UPDATE:

I now realize what you mean and I need this. Is there a workaround for this?

My workaround is:

models.py

from partial_index import PQ, PartialIndex, ValidatePartialUniqueMixin
from versions.models import Versionable

class VersionedWBSElement(ValidatePartialUniqueMixin, Versionable):
    # wbs number is unique regardless
    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))
        ]

serializers.py

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant