django-taggit-serializer icon indicating copy to clipboard operation
django-taggit-serializer copied to clipboard

'Invalid json list. A tag list submitted in string form must be valid json.

Open pyprism opened this issue 7 years ago • 20 comments

When I try to create object from test client I get this error {'tag': ['Invalid json list. A tag list submitted in string form must be valid json.']}. Here is the code:

self.client.post('/api/notes/', data={'tag': ["xyz"], 'iv': 'random', 'content': "New content",
                                                         'date': self.current_date_time})

pyprism avatar Jan 07 '17 13:01 pyprism

Can you try it with:

import json

self.client.post('/api/notes/', 
                        data=json.dumps({
                            'tag': ["xyz"], 
                            'iv': 'random', 'content': "New content",
                            'date': self.current_date_time
                         }))

to check if this "post" only translate the dict into a str instead of a valid JSON-String

RafaAguilar avatar Jan 09 '17 20:01 RafaAguilar

Here is the response I got after following your suggestion

{'non_field_errors': ['Invalid data. Expected a dictionary, but got str.']}

pyprism avatar Jan 13 '17 12:01 pyprism

just to make sure, these are the tests correct?

What about adding the kwarg format (I just want to make sure format is correct)

self.client.post('/api/notes/', data={'tag': ["xyz"], 'iv': 'random', 'content': "New content",
                                                         'date': self.current_date_time}, format='json')

Let me know if that did anything!

glemmaPaul avatar Jan 17 '17 11:01 glemmaPaul

I am already using format='json'.

pyprism avatar Jan 17 '17 12:01 pyprism

@pyprism are you able to use it by requesting via HTTP, e.g Postman?

glemmaPaul avatar Feb 09 '17 09:02 glemmaPaul

Hi, Same behavior for me with the last (actual) DRF 3.6

{
    "tags": [
        "Invalid json list. A tag list submitted in string form must be valid json."
    ]
}

foxmask avatar Mar 17 '17 09:03 foxmask

@glemmaPaul It seems like serializer is having trouble with Querydict. To deal with multiple values for the same key, Querydict stores each value with the same key in a list. Also, a JSON array treated as a set of values, so its items are stored in a single list. So, above code will generate Querydict like below: <QueryDict: {'tag': ['xyz'], 'iv': ['random'], 'content': ['New content'], 'date': [...]}> And, Querydict returns the last value when accessing for a key. So, when serializer retrieves the item for a key 'tag', it will get 'xyz', not ['xyz'].

ppnchb avatar Jun 09 '17 17:06 ppnchb

I found the forum article says request.data is sometimes QueryDict, sometimes normal dict. Maybe converting request.data from QueryDict to dict in views.py can temporally solve the problem..

ppnchb avatar Jun 09 '17 18:06 ppnchb

I have the same problem, any solutions?

kamilgregorczyk avatar Apr 16 '18 17:04 kamilgregorczyk

I'm also having the same problem?

adnanmuttaleb avatar Jul 10 '18 18:07 adnanmuttaleb

I'm also having the same problem

linpan avatar Sep 23 '18 08:09 linpan

I ran into the same problem

hsbzzhz avatar Oct 16 '18 23:10 hsbzzhz

Was there ever a resolution to this? It is still a prevalent issue with tags in DRF 3.0+ @hsbzzhz @linpan @glemmaPaul

lowkeyshift avatar Mar 18 '19 01:03 lowkeyshift

I found a temp solution. I think this is caused by python str is single quote by default but json use double quotes data={'tag': str(["xyz"]).replace("'", '"')} will work

renguomin avatar Mar 27 '19 17:03 renguomin

I found a temp solution. I think this is caused by python str is single quote by default but json use double quotes data={'tag': str(["xyz"]).replace("'", '"')} will work

This works for me as well. Thanks! The API itself also seems to work, when called with Postman.

robertsiipola avatar Jun 22 '20 05:06 robertsiipola

IS there a proper solution now? It worked for me when I gave it as list ["tag1", "tag2]"..

jerrypaulsam avatar Aug 02 '20 09:08 jerrypaulsam

I have the same problem Is there a proposed soltion ? Thanks

nadhem-zmandar avatar Dec 18 '20 18:12 nadhem-zmandar

I have the same problem Is there a proposed soltion ? Thanks

[ "tag1", "tag2" ]

This worked for me

SauravKanchan avatar Jan 02 '21 18:01 SauravKanchan

The same problem here. Any solution?

narteyjulius avatar Dec 16 '21 17:12 narteyjulius

it's literally one post above, don't send a value but an array of values

kamilgregorczyk avatar Dec 17 '21 08:12 kamilgregorczyk