Home > Mobile >  Django Rest framework - data between two serializers do not validate
Django Rest framework - data between two serializers do not validate

Time:10-16

I have a API view in place where I first want to create a new user (already working) and second I want to return the new created user object using my UserSerializer (Not working).

views.py

@api_view(['POST'])
@permission_classes([AllowAny])
def user_create(request):
    exception_handler = UserUnavailable
    success_handler = UserCreated
    if request.method == 'POST':
        creation_serializer = CreateUserSerializer(data=request.data)
        try:
            if creation_serializer.is_valid(raise_exception=True):
                creation_serializer.save()
                user_serializer = UserSerializer(data=creation_serializer.instance.id)
                if user_serializer.is_valid():
                    return JsonResponse({"status_code": success_handler.status_code,
                                         "default_detail": success_handler.default_detail,
                                         "default_code": success_handler.default_code,
                                         "new_user": user_serializer,
                                         }, safe=False)
        except APIException:
            return JsonResponse({"status_code": exception_handler.status_code,
                                 "default_detail": exception_handler.default_detail,
                                 "default_code": exception_handler.default_code
                                 }, safe=False)

I can confirm that creation_serializer.instance.id contains the new users id.

serializers.py

class UserSerializer(serializers.ModelSerializer):
    id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())

    class Meta:
        model = get_user_model()
        fields = ('id', 'user')
        read_only_fields = ('id', 'user')

I can also confirm that

if user_serializer.is_valid() 

does not validate for some reason. Any ideas

Thanks in advance

CodePudding user response:

To initialize a serializer with user instance instead of

 user_serializer = UserSerializer(data=creation_serializer.instance.id)

You should write something like this:

user_serializer = UserSerializer(instance=creation_serializer.instance)

instead of

Also, you don't have to validate the object that has already been saved. And create a response you should like this:

return JsonResponse({
    "status_code": success_handler.status_code,
    "default_detail": success_handler.default_detail,
    "default_code": success_handler.default_code,
    "new_user": user_serializer.data,
}, safe=False)

But using api_view is now a bad thing. It's much better to use ModelViewSet. You can use something like this:

class UserViewSet(ModelViewSet):
    serializer_class = UserSerializer
    queryset = User.objects.all()

    def get_serializer_class(self, *args, **kwargs):
        if self.action == 'create':
            return CreateUserSerializer
        return super().get_serializer_class(*args, **kwargs)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)

        response_serializer = self.serializer_class(instance=serializer.instance)
        return Response(response_serializer.data, status=status.HTTP_201_CREATED, headers=headers)

And I'm sorry, I haven't tested it, there might be typos. You can read about ModelViewsets more here https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset

  • Related