Skip to content
Snippets Groups Projects
Commit aeef4000 authored by Valentin Rigal's avatar Valentin Rigal
Browse files

Add the condition to the serializer

parent 5ad81f36
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,7 @@ from collections import defaultdict
from textwrap import dedent
from django.db import transaction
from django.db.models import Count, Q
from django.db.models import Count, Exists, OuterRef, Q
from drf_spectacular.utils import extend_schema_field
from rest_framework import permissions, serializers
from rest_framework.exceptions import PermissionDenied, ValidationError
......@@ -575,6 +575,17 @@ class DatasetSerializer(serializers.ModelSerializer):
raise ValidationError("Either do not specify set names to use the default values, or specify a non-empty list of names.")
return set_names
def validate_unique_elements(self, unique):
if unique is True and self.instance and Exists(
DatasetElement.objects
.filter(set__dataset_id=OuterRef(self.instance.pk))
.values("element")
.annotate(dups=Count("element"))
.filter(dups__gte=2)
):
raise ValidationError("Elements are currently contained by multiple sets.")
return unique
def validate(self, data):
data = super().validate(data)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment