Code

Opened 5 years ago

Closed 5 years ago

Last modified 14 months 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: UI/UX:

Description (last modified by kmtracey)

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.

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by kmtracey

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 14 months ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.