DRF: Validate nested serializer data when creating, but not when updating
What I really miss in both solutions is that the validation error is not attached to the field name of the model Genre and the user input in the form is lost.
This is straight forward:
from rest_framework import exceptions
class BookViewSet(viewsets.ModelViewSet):
....
def perform_create(self, serializer):
if check_failed():
raise exceptions.ValidationError(
exceptions._get_error_details({
'genre': {
'name': ['must be unique']
}
})
)
This is how I did it:
class GenreSerializer(serializers.ModelSerializer):
# ... snip ...
def validate_name(self, value):
if self.context['request']._request.method == 'POST':
if self.Meta.model.objects.filter(name=value).exists():
raise ValidationError('A genre with this name already exists.')
return value
In this way the validation is triggered only when a new Genre
object is created (POST
), not when it is updated (PUT
).
When a new Book
object is created, the validation for Genre
is propagated to the nested serializer.
All form inputs are preserved after validation and the error message is attached to the field name
.
That actually fulfills all my criteria. Although I don't have the feeling that this is the right way of doing it. I'd still like to know how could I call manually the UniqueValidator
in validate_name
, instead of reinventing that validation.
EDIT:
I found out a way how to call the UniqueValidator
in the method:
def validate_name(self, value):
if self.context['request']._request.method == 'POST':
unique = UniqueValidator(
self.Meta.model.objects.all(),
message='Genre with this name already exists.'
)
unique.set_context(self.fields['name'])
unique(value)
return value
EDIT (2022-09-09):
Instead of self.context['request']._request.method
it's much simpler and cleaner to use self.context['request'].method
.
Tried and tested with Django REST Framework 3.13.