kayode-adechinan
9/25/2018 - 5:42 PM

Example of returning a Django QuerySet as a JSON response (Django 1.7)

Example of returning a Django QuerySet as a JSON response (Django 1.7)

# 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')