5

As stated in DRF documentation http://www.django-rest-framework.org/api-guide/parsers/#multipartparser, in order to parse multipart/form-data, the MultiPart and form parser must be used. I have a supiscion this is a problem in the Django Rest Framework because I saw a solution on their github issue saying it work using APIView.

from django.contrib.auth.models import User
from rest_framework import viewsets
from api.serializers import UserSerializer,

from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
from rest_framework import status

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all()
    parser_classes = (MultiPartParser, FormParser)
    serializer_class = UserSerializer

Picture of me sending request to Postman with result: Picture of me sending request to Postman with result

Edit: Adding UserSerializer class

class UserSerializer(serializers.HyperlinkedModelSerializer):
    snapcapsules = SnapCapsuleSerializer(
        many=True,
        read_only=True,
        allow_null=True,
    )

    class Meta:
        model = User
        fields = ('snapcapsules', 'url', 'username', 'email', 'password', )
        write_only_fields = ('password',)

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

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

        return user

    def update(self, instance, validated_data):
        capsules_data = validated_data.pop('snapcapsules')

        for capsule in capsules_data:
             SnapCapsule.objects.create(user=instance, **capsule)

        return instance

Picture of what I of what I requested

4
  • Show UserSerializer please. Commented Jun 21, 2018 at 1:50
  • cat u post the post body, because if u set request header multipart/form-data,u can just post data with the postman form-data rather than x-www-form-urlencoded Commented Jun 21, 2018 at 1:51
  • I don't understand what you are doing here. You've set the header to multipart/forn-data, but then what you're actually sending is clearly JSON and not form data at all. Commented Jun 21, 2018 at 7:06
  • @WaketZheng I posted the edit Commented Jun 21, 2018 at 18:21

2 Answers 2

3

This is might not the issue with the ContentType. The error response says that,the UserSerializer excpect a payloadordata which include username and password field

So, Please try to add those fields to the request body and try again POstman ScreenShot

UPDATE

The problem with the Orginal Post was, he added extra headers in POSTMAN (see the pic below)

enter image description here.

Postman will add appropriate Headers aromatically for you. So, you don't have to explicitly mention it

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I had something in the Post Body, but I forgot to add the picture. You can see what I sent with the results above now.
Well, then I recommend you to open a new POSTMAN tab and repeat the procedure and also do not manually provide anything on Headers tab
Postman will add appropriate Headers aromatically for you. So, you don't have to explicitly mention it
2

I removed one line and then it worked!

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

Also, the serializer can be rewrite as follows:

class UserSerializer(serializers.HyperlinkedModelSerializer):
    ...

    def create(self, validated_data):
        return User.objects.create_user(
            username=validated_data['username'],
            email=validated_data['email'],
            password=validated_data['password']
        )

    ...

2 Comments

I tried your solution and it did not work. Are you using the multipart parser because the documentations say you need to in order to process multipart/form-data.
When I change my headers to empty, your solutions works. I upvoted your for this reason. Thank you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.