Opened 15 years ago
Last modified 12 years ago
#11802 closed
Wrong use of aggregation module may lead to non-deterministic bugs — at Initial Version
Reported by: | AmirHabibi | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.1 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Assuming that an AJAX query posts (start, limit) for retrieving Polls (Django tutorial), writing the following code leads to request.POST problem.
from django.core import serializers
from django.http import HttpResponse
from django.db.models import Count
from polls import Poll
def respond(rows, totalCount):
data = serializers.serialize('json', rows)
response = "{'success': true, 'totalCount':%d, 'rows':%s }" % (totalCount, data)
return HttpResponse(response, mimetype="application/javascript")
def listPolls(request):
rows = Vehicle.objects.all()[start:start+limit]
totalCount = Vehicle.objects.aggregate(Count('id'))
return respond(rows, totalCount)
surprisingly, fixing the code to:
totalCount = Vehicle.objects.aggregate(Count('id'))id__count
fixes the request.POST exception. The issue is that the django error report about request.POST not having a key named 'start' or 'limit' distracts the programmer to debugging the front end AJAX code rather than focusing on the Python bug. It also may indicate a more fundamental issue that exhibits random behavior.