beda-software / drf-writable-nested

Writable nested model serializer for Django REST Framework
Other
1.08k stars 116 forks source link

MultiValueDictKeyError at /users/ #6

Open danilke opened 7 years ago

danilke commented 7 years ago

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

MultiValueDictKeyError at /users/
"'profile'"
ruscoder commented 7 years ago

@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.

danilke commented 7 years ago

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 commented 7 years ago

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!

rainyx commented 6 years ago

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

Some MultiValueDict data like this:

data = {
   'a.title': 'xxxxx',
   'a.desc': 'vvvvvv'
}
d3dc commented 6 years ago

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)

ruscoder commented 6 years ago

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

auvipy commented 4 years ago

is it fixed?

auvipy commented 3 years ago

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