﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
11802	Wrong use of aggregation module may lead to non-deterministic bugs	AmirHabibi		"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):
    start = request.POST['start']
    limit = request.POST['limit']

    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.
"		new	ORM aggregation	1.1					Unreviewed	0	0	0	0	0	0
