Asinox
Asinox

Reputation: 6865

How to get data from a Django JsonField?

I would like to know how I can get ("decode?") the data from a JsonField, I'm having this:

{"pleople": "name=Jhon&[email protected]", "id": 251304}

How I can pass this to my view like name['Jhon'] or any kind of object to use with querySet or parameter?

Upvotes: 1

Views: 1021

Answers (1)

okm
okm

Reputation: 23871

>>> from urlparse import parse_qs, parse_qsl

>>> parse_qs("name=Jhon&[email protected]")
{'email': ['[email protected]'], 'name': ['Jhon']} # allow multiple values

>>> dict(parse_qsl("name=Jhon&[email protected]"))
{'email': '[email protected]', 'name': 'Jhon'} # dict w/ single value

Or you could use django.http.QueryDict directly

>>> from django.http import QueryQict
>>> QueryDict("name=Jhon&[email protected]")
<QueryDict: {u'name': [u'Jhon'], u'email': [u'[email protected]']}>

Upvotes: 1

Related Questions