Skip to content

Instantly share code, notes, and snippets.

@cspanring
Created April 15, 2013 15:38
Show Gist options
  • Save cspanring/5389030 to your computer and use it in GitHub Desktop.
Save cspanring/5389030 to your computer and use it in GitHub Desktop.
Basic example for iterating over a Django queryset and returning JSON objects.
import json
from .models import MyModel
def get_json(request):
# Return JSON for filtered MyModel objects
records = MyModel.objects.filter(myproperty=myvalue)
json_res = []
for record in records:
json_obj = dict(
myproperty = record.myproperty,
)
json_res.append(json_obj)
return HttpResponse(json.dumps(response), mimetype='application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment