[Fixed]-Django Serialize Queryset to JSON to construct RESTful response with only field information and id

26👍

What you want to achieve is subset of fields dumped to json.

What you’re doing is serializing whole django’s ORM objects. Not good.

Keep it simple:

import json

posts = (Post.objects.filter(owner=authenticated_user)
                     .values('id', 'title', 'summary'))
json_posts = json.dumps(list(posts))

Leave a comment