Forked from leemac/DjangoQuerySetToJsonResponse.py
Created
September 25, 2018 17:42
-
-
Save kayode-adechinan/039241ddf1591f43e593287556c1e4b8 to your computer and use it in GitHub Desktop.
Example of returning a Django QuerySet as a JSON response (Django 1.7)
This file contains 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
# Get the objects from the database | |
rawData = Launch.objects.all() | |
# Create array | |
json_res = [] | |
# Iterate over results and add to array | |
for record in rawData: | |
json_obj = dict(name = record.name) | |
json_res.append(json_obj) | |
# Return the results | |
return HttpResponse(json.dumps(json_res), content_type='application/json') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment