Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Input is not a valid dict" when posting to a model with file attribute #21

Open
GuillaumeCisco opened this issue Jun 23, 2017 · 7 comments

Comments

@GuillaumeCisco
Copy link

Hello I've just detected that we get the error :

"Input is not a valid dict"

when posting with requests.post with a files attribute.
the file attribute will force the request as a multipart formencoded, so the data will be json stringified.

I've created a pull request for handling this behavior.
#20

@vdboor
Copy link
Contributor

vdboor commented Jul 4, 2017

Thanks for the PR! As mentioned in #20, do you have an example how to cause this error? For example, a curl command or Django unit test?

@GuillaumeCisco
Copy link
Author

Yes I was using a curl command like:
curl -H "Content-Type:multipart/form-data" -F translations="{\"en\": {\"display_name\": \"toto\"}}" -F name="test" -F type="3" -F "file=@./toto;type=application/json" http://my_domain.com/my_ressource/

@crstian
Copy link

crstian commented Apr 8, 2020

Hi. This issue still exists - is there any update regarding this error?

@crstian
Copy link

crstian commented Sep 25, 2020

What I did to make it work was using json.loads(data) to format the submitted form data. Unfortunately django-parler is checking for isinstance(data, dict) (line 128, here) which returns false on JavaScript objects that where submitted by form data and previous stringified with json.stringify.

Hope this helps someone.

@MehdiDRISSIB
Copy link

Really helpful @crstian . I had the same issue and solved quickly with your solution !

@EliasOPrado2
Copy link

EliasOPrado2 commented Aug 25, 2021

I have added the change that was done by @GuillaumeCisco but the code doesn't appear to work:
Is there anything I am doing wrong?

    ...
 if isinstance(data, str):
     # try to convert to json
     try:
         data = json.loads(data)
      except:
          self.fail('invalid_json_dump')
    ...

@MehdiDRISSIB
Copy link

MehdiDRISSIB commented Mar 6, 2023

You should overload TranslatedFieldsField and call the overloaded class in your serializer like this:

import json
from parler_rest.fields import TranslatedFieldsField
from rest_framework import serializers


class TranslatedFieldFixed(TranslatedFieldsField):

    def to_internal_value(self, data):
        """
        Deserialize data from translations fields.
        For each received language, delegate validation logic to
        the translation model serializer.
        """
        if data is None:
            return
        data = json.loads(data)
        if not isinstance(data, dict):
            self.fail('invalid')
        if not self.allow_empty and len(data) == 0:
            self.fail('empty')

        result, errors = {}, {}
        for lang_code, model_fields in data.items():
            serializer = self.serializer_class(data=model_fields)
            if serializer.is_valid():
                result[lang_code] = serializer.validated_data
            else:
                errors[lang_code] = serializer.errors

        if errors:
            raise serializers.ValidationError(errors)
        return result

But It would be best if the error is fixed directly in the library

@GuillaumeCisco GuillaumeCisco changed the title "Input is not a valid dict" when posting to a model witl file attribute "Input is not a valid dict" when posting to a model with file attribute Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants