0

create user django post request

I'm new in Django and DRF and I'm trying to create a new user through POST request. I send the username and password parameters but django doesn't identify the parameters inside the POST request.

My serializer:

class UserSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True)

    def create(self, validated_data):
        user=get_user_model().objects.create(
        username = validated_data['username']
    )

        user.set_password(validated_data['password'])
        user.save()
        return user

    class Meta:
        model = get_user_model()
        fields = ('username', 'password')

My view class:

class CreateUserView(CreateAPIView):
    model = get_user_model()
    permission_classes = (AllowAny,)
    serializer_class = UserSerializer

I have tried to use serializers.ModelSerializer and serializer.Serializers but don't have success.

2
  • 2
    You are adding the parameters to the querystring, so they should be accessible from request.GET. Instead, include them as json in the request body and so they are included in validated_data.
    – fips
    Mar 3, 2017 at 13:22
  • Your screenshot seems suspicious as it looks like it's not posting data but passing them as url parameters. Open your browser's dev tool and look at the request posted data to insure data are really posted in the HTTP body and not in the url.
    – Linovia
    Mar 3, 2017 at 14:25

2 Answers 2

0
class UserSerializer(serializers.ModelSerializer):
    
    def create(self, validated_data):
        user = UserModel.objects.create_user(**validated_data)

        return user


    class Meta:
        model = YOUR_MODEL
        fields = ('username', 'password',)



class CreateUserView(CreateAPIView):
    model = YOUR_MODEL.objects.all()
    permission_classes = (AllowAny,)
    serializer_class = UserSerializer

You can do View as this and check your errors.

view.py

class CreateUser(APIView):
    permission_classes = (AllowAny,)

    def post(self, request):
        post_data = request.data

        print post_data # See what is your post DATA


        serializer = UserSerializer(data=post_data)

        if serializer.is_valid(raise_exception=True):
            serializer.save()
            return Response(data={'user':serializer.data})
        return Response(data={'user':serializer.errors})
2
  • Thanks for asking. But I've got the same error: "username": ["This field is required."] :( Mar 3, 2017 at 13:22
  • Ok then you must provide username in your json post.
    – Marin
    Mar 3, 2017 at 13:28
0

You are trying to send the username and password fields as a query param in your request. What your APIView is expecting is username and password to be sent as a request body. So, pass these fields in json body in Postman like this

{
   username : "your username",
   password : "your password"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.