18

I currently have a Post model with 'title' and 'summary' fields. I'm retrieving all the Posts and returning them as JSON as part of a RESTful API interface.

Here's the basic approach

from django.core import serializers

def list_posts(request):
    posts = Post.objects.filter(owner=authenticated_user)
    serialized = serializers.serialize("json", posts, fields=('title', 'summary'))
    return HttpResponse(serialized, mimetype='application/json')

And I'm getting the following response back when I visit the corresponding route.

Current Response

[{"pk": 4, "model": "api.post", "fields": {"summary": "Testing", "title": "My Test"}}, {"pk": 5, "model": "api.post", "fields": {"summary": "testing again", "title": "Another test"}}]

This technically contains all the information my client-side needs to construct models (I'm using Backbone and could use collection.parse to construct what I need, but the server side should be responsible for structuring the response nicely). What troubles me about this is that it does not look like the standard API responses I'm used to seeing in reputable APIs. I think a JSON response like the following would be more 'standard'.

Desired Response

[{'summary': 'Testing', 'id': 4, 'title': 'My test'}, {'summary': 'My Test', 'id':5, 'title': 'Another test'}]

The output from serialize does not seem quite right for returning a collection of model instances in JSON as a response from an API call and this seems like a fairly common need. I'd like to return the fields information along with the id (or pk, if it must be called pk).

2
  • 1
    Check out the tastypie project. It provides a convenient way to add an API to your Django models and will serialize to output more like what you are expecting. Commented Jan 21, 2013 at 3:06
  • I ended up spending a few hours integrating tastypie into my API and experimenting with it before switching to the Django REST Framework which was better suited for my project needs. IMO is a bit more flexible and well documented. Tastypie is still a great suggestion and a great REST framework for some applications. Thanks.
    – dgh
    Commented Jan 21, 2013 at 17:52

1 Answer 1

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))
5
  • Ah, so posts is a Django ValueQuerySet which returns dictionaries upon iteration. Haven't tried this, but it seems useful. Does this lose the advantage using the Django serializers had of automatically serializing all field types? I think json.dumps hickups on DateTimeField and other fields, but I haven't experimented with this.
    – dgh
    Commented Jan 21, 2013 at 18:01
  • 2
    For this, my friend, lean into Django REST Framework. Commented Feb 4, 2016 at 15:09
  • What would be a good way to convert a queryset to a list of JSON objects, add a new field to each JSON object and return the new list in the response? Commented Mar 22, 2017 at 11:27
  • 1
    @DivijSehgal django-rest-framework.org/api-guide/viewsets/#modelviewset Commented Mar 23, 2017 at 8:41
  • @KrzysztofSzularz did it that way :) Thanks again :) Commented Mar 23, 2017 at 11:04

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.