From c32bdbd59b4b993dcededa1f5e2b6cc9452f83bf Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Mon, 21 Oct 2024 19:05:26 -0400 Subject: [PATCH] Reraise validation errors --- arches_lingo/views/api/pythonic_models.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/arches_lingo/views/api/pythonic_models.py b/arches_lingo/views/api/pythonic_models.py index f27990a5..184a8394 100644 --- a/arches_lingo/views/api/pythonic_models.py +++ b/arches_lingo/views/api/pythonic_models.py @@ -1,5 +1,7 @@ from functools import partial +from django.core.exceptions import ValidationError as DjangoValidationError +from rest_framework.exceptions import ValidationError from rest_framework.permissions import IsAuthenticated from rest_framework.generics import RetrieveUpdateDestroyAPIView @@ -26,6 +28,27 @@ def update(self, request, *args, **kwargs): self.get_object = partial(self.get_object, user=request.user) return super().update(request, *args, **kwargs) + def perform_update(self, serializer): + """Re-raise ValidationError as DRF ValidationError. + + In 3.0 (2014), DRF decided to stop full_clean()'ing before save(), + which divorces DRF validation needs from model logic needing to + support the Django admin or similar ModelFormish patterns. + The stated reasons were: + - to avoid needing to know about big & scary full_clean(). Fine. + - to force expressing validation logic outside of models. + but to adhere to *that* here would require some way of generically + validating incoming tile data without knowledge of the resource/graph, + which isn't practical under this implementation. + + Discussion: + https://github.com/encode/django-rest-framework/discussions/7850 + """ + try: + serializer.save() + except DjangoValidationError as django_error: + raise ValidationError(detail=django_error) from django_error + class SchemeDetailView(PythonicModelAPIMixin, RetrieveUpdateDestroyAPIView): permission_classes = [IsAuthenticated]