2

In a view of django, I want to output the queryset converted to json without the model, pk, and field text.

my view code:

s = serializers.serialize('json', Item.objects.get(id=actuators_id)])
o = s.strip("[]")
return HttpResponse(o, content_type="application/json")

What I get is this:

{"model": "actuators.acutatoritem", "pk": 1, "fields": {"name": "Environment Heater", "order": 1, "controlid": "AAHE", "index": "1", "param1": "", "param2": "", "param3": "", "current_state": "unknown"}}

What I spend all day NOT getting is this:

{"name": "Environment Heater", "order": 1, "controlid": "AAHE", "index": "1", "param1": "", "param2": "", "param3": "", "current_state": "unknown"}

I can I strip the model, pk, and field text from my output????

1 Answer 1

1

Use simplejson to convert the qs to a python dict {}

import simplejson
s = serializers.serialize('json', Item.objects.filter(id=actuators_id)])

js = simplejson.loads(s)
//select the key needed and return the response   
s = js[0]['fields']

return HttpResponse(str(s), content_type="application/json")
1
  • OMG. I spent about 4 hours on this task. Thanks! Jul 22, 2016 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.