diff --git a/django/hello/world/views.py b/django/hello/world/views.py index b020922e03c..0d1dbbafbd6 100644 --- a/django/hello/world/views.py +++ b/django/hello/world/views.py @@ -4,22 +4,25 @@ from django.http import HttpResponse from django.core import serializers from world.models import World -import simplejson +# json is available on any real production environment and better +import json +#import simplejson import random def json(request): - response = { - "message": "Hello, World!" - } - return HttpResponse(simplejson.dumps(response), mimetype="application/json") + return HttpResponse(json.dumps({"message" : "Hello, World!"), mimetype="application/json") def db(request): queries = int(request.GET.get('queries', 1)) - worlds = [] - - for i in range(queries): - # get a random row, we know the ids are between 1 and 10000 - worlds.append(World.objects.get(id=random.randint(1, 10000))) + # make your id's all at once unless it's cheating + ids = [random.randint(1, 10000) for x in range(queries)] + # if it's cheating to run only one DB query, no worries + # my async changes in the DB setup will compensate nicely + # get random rows, we know the ids + + # If the id's aren't needed, use the power of the ORM + # get the stuff you need only with .values_list('randomNumber', flat=True) + worlds = World.objects.filter(id__in=ids)) return HttpResponse(serializers.serialize("json", worlds), mimetype="application/json")