Commits

Sergiy shchypa committed 03dd3a7

better json implementation

  • Participants
  • Parent commits 31fc148

Comments (0)

Files changed (1)

cmsplugin_poll/views.py

     c = RequestContext(request, {'poll': poll})
     return render_to_response('cmsplugin_poll/results.html', c)
 
+def clean_json(json_string):
+    """
+    delete internal data about pk and models name ''
+    """
+    data = simplejson.loads(json_string)
+
+    for d in data:
+        del d['pk']
+        del d['model']
+    return data
 
 def results_json(request, poll_id):
     polls = Poll.objects.filter(pk=poll_id)
-    polls_data = serializers.serialize("json", polls)
+    polls_data = serializers.serialize("json", polls, fields=('votes','pub_date','question'))
+    polls_data = clean_json(polls_data)
 
     choices = Choice.objects.filter(poll=poll_id)
     choices_data = serializers.serialize("json", choices)
+    choices_data = clean_json(choices_data)
 
     data = {'poll': polls_data, 'choices': choices_data}
-    return HttpResponse(simplejson.dumps(data), mimetype="application/json")
+    return HttpResponse(simplejson.dumps(data))