Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using stock json and improved query. Query might be cheating. #21

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions django/hello/world/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")