Opened 15 years ago

Closed 15 years ago

Last modified 11 years ago

#11802 closed (invalid)

Wrong use of aggregation module may lead to non-deterministic bugs

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 (last modified by Karen Tracey)

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.

Change History (2)

comment:1 by Karen Tracey, 15 years ago

Description: modified (diff)
Resolution: invalid
Status: newclosed

Fixed formatting -- please use preview.

I don't see the Django bug here. Your code had an error and you got some kind of exception. You didn't include the details of the exception so it is difficult for anyone to explain why you got the exception you got instead of what you might have been expecting. "Random behavior" is extremely unlikely. If you care to post a complete example including details of the exception you received on django-users, then someone may be able to explain why you got what you got. But without a specific suggestion for something to improve in Django this is not a valid ticket.

comment:2 by Anssi Kääriäinen, 11 years ago

Component: ORM aggregationDatabase layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top