Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserialize list of nested dictionaries to a simpler form
    text
    copied!<p>How I can deserialize this : </p> <pre><code>[{'fields': {'active': 1, 'image': 'http://img.rasset.ie/0003db75-150.jpg', 'name': 'Bad Fellas', 'position': 0, 'url': 'http://www.rte.ie/tv/programmes/bad_fellas.html'}, 'model': 'rte_site.show', 'pk': 1}, {'fields': {'active': 1, 'image': 'http://img.rasset.ie/0002c8d0-250.jpg', 'name': 'Crimecall', 'position': 0, 'url': 'http://www.rte.ie/tv/crimecall/'}, 'model': 'rte_site.show', 'pk': 2}] </code></pre> <p>to form :</p> <pre><code>{'1': {'pk': 1, "url": "http....", "image": 'http://....', 'name': 'Bad Fellas'}, '2': {...}} </code></pre> <p>? Is it even possible to strip it this way ?</p> <hr> <p>EDIT I've tried two solutions and I'm getting :</p> <p><code>string indices must be integers, not str</code></p> <p>here's the link : <a href="http://ntt.vipserv.org/data/shows" rel="nofollow">http://ntt.vipserv.org/data/shows</a></p> <p>and my whole code of django function:</p> <pre><code>def shows_json(request): objects = Show.objects.all() tmp = toJSON(objects) l = [{"pk": d["pk"], "url": d["fields"]["url"], "image": d["fields"]["image"], "name": d["fields"]["name"]} for d in tmp] json = dict(str((d["pk"]), d) for d in l) result = render_to_string('jsonlist.html', RequestContext(request, { 'json': json, })) return HttpResponse(result) def toJSON(obj): if isinstance(obj, QuerySet): return simplejson.dumps(obj, cls=DjangoJSONEncoder) if isinstance(obj, models.Model): set_obj = [obj] set_str = simplejson.dumps(simplejson.loads(serialize('json', set_obj))) str_obj = set_str[1:len(set_str)-2] return str_obj from django.core.serializers import serialize from django.utils.simplejson import dumps, loads, JSONEncoder from django.db.models.query import QuerySet from django.utils.functional import curry class DjangoJSONEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, QuerySet): return loads(serialize('json', obj)) return JSONEncoder.default(self,obj) dumps = curry(dumps, cls=DjangoJSONEncoder) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload