When using writeable nested serializers in DRF, there is a known problem with checking for possible unique fields and preventing the parent serializer from updating. This problem was repeatedly asked in such questions:
For simplicity, we take an example from the first question:
class GenreSerializer(serializers.ModelSerializer): class Meta: fields = ('name',)
Now the problem is that the validation of uniqueness also falls for creation. This can be intercepted in a view, for example:
class BookViewSet(viewsets.ModelViewSet): queryset = Book.objects.all() serializer = BookSerializer def perform_create(self):
However, this is not entirely correct, because we test the uniqueness of Genre in BookViewSet .
Another option is to implement verification in the create() BookSerializer , as explained in the second question (see the list above).
What I really missed in both solutions is that the validation error is not tied to the name field of the Genre model and user input in the form is lost.
I would like to add a validation error for Genre.name to existing validation errors, save the user login and only do this for creation, not for updating.
My ideas were something like this:
class GenreSerializer(serializers.ModelSerializer):
Is this possible or is there any other way to achieve the above goal - to save the userโs input and add the verification error attached to the name field to the list of existing verification errors when creating a new instance?