drf-writable-nested icon indicating copy to clipboard operation
drf-writable-nested copied to clipboard

MultiValueDictKeyError at /users/

Open danilke opened this issue 7 years ago • 8 comments

Hi, got problem with nested serializer: On request POST:

MultiValueDictKeyError at /users/
"'profile'"

danilke avatar May 10 '17 09:05 danilke

@danilke Hello! Could you please provide more information? I'm intrested in the samples of serializer's code and the samples of data which you send.

ruscoder avatar May 10 '17 09:05 ruscoder

Thank you for so fast feedback! Looks like problem is in my low skill in django, because I have tried to save object from console with UserSerializer, and it went fine.

models.py

class Profile(models.Model):
    user = models.OneToOneField(User)
    city = models.CharField(max_length=50, blank=True, default='')
    phone = models.CharField(max_length=50, blank=True, default='')

serializers.py

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = ('pk', 'city', 'phone',)


class UserSerializer(WritableNestedModelSerializer):
    profile = ProfileSerializer(allow_null=True, many=False)

    class Meta:
        model = User
        fields = (
            'pk',
            'url',
            'username',
            'email',
            'groups',
            'profile',
            )

Where can be problem? One more tine thanks a lot!

danilke avatar May 10 '17 09:05 danilke

Hm, I was trying to send data from "browsable api" in DRF, and its went wrong, but when I've sended json from postman, was saved without any error :) Thank you for nice library :) It saved me a lot of time!

danilke avatar May 10 '17 10:05 danilke

Same problem, when using multipart formatted data, MultiValueDictKeyError will be raised.

Some MultiValueDict data like this:

data = {
   'a.title': 'xxxxx',
   'a.desc': 'vvvvvv'
}

rainyx avatar Dec 25 '17 10:12 rainyx

I'm hitting this issue in my code

drf_writable_nested/mixins.py", line 122, in update_or_create_reverse_relations
    related_data = self.initial_data[field_name]

MultiValueDictKeyError: "'offices'"

 

As @rainyx mentioned, this seems to happen with multipart form data.

I'm sending a partial update with the body:

------WebKitFormBoundaryBxUozciZvqZAB7cj
Content-Disposition: form-data; name="org_image"; filename="Group.png"
Content-Type: image/png

------WebKitFormBoundaryBxUozciZvqZAB7cj--

In my serializer, however, I get extra validated_data:

{ 'org_image': <InMemoryUploadedFile: Group.png (image/png)>, 'offices': [] }

(offices is a writable nested ManyToMany with a standard serializer)

d3dc avatar Mar 20 '18 00:03 d3dc

@d3dc Hello! Can you check your issue with v0.4.3?

ruscoder avatar Jul 04 '18 11:07 ruscoder

is it fixed?

auvipy avatar Nov 17 '19 10:11 auvipy

this can be closed based on https://github.com/beda-software/drf-writable-nested/pull/42#issuecomment-400545941

auvipy avatar Oct 15 '21 14:10 auvipy