Created
April 15, 2013 15:38
-
-
Save cspanring/5389030 to your computer and use it in GitHub Desktop.
Basic example for iterating over a Django queryset and returning JSON objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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