django


Can't query the sum of values using aggregators


I want to sum the values of all existing rows grouping by another field.
Here's my model structure:
class Answer(models.Model):
person = models.ForeignKey(Person)
points = models.PositiveIntegerField(default=100)
correct = models.BooleanField(default=False)
class Person(models.Model):
# irrelevant model fields
Sample dataset:
Person | Points
------ | ------
4 | 90
3 | 50
3 | 100
2 | 100
2 | 90
Here's my query:
Answer.objects.values('person').filter(correct=True).annotate(points_person=Sum('points'))
And the result (you can see that all the person values are separated):
[{'person': 4, 'points_person': 90}, {'person': 3, 'points_person': 50}, {'person': 3, 'points_person': 100}, {'person': 2, 'points_person': 100}, {'person': 2, 'points_person': 90}]
But what I want (sum of points by each person):
[{'person': 4, 'points_person': 90}, {'person': 3, 'points_person': 150}, {'person': 2, 'points_person': 190}]
Is there any way to achieve this using only queryset filtering?
Thanks!
Turns out I had to do the inverse filtering, by the Person's and not the Answers, like so:
Person.objects.filter(answer__correct=True).annotate(points=Sum('answer__points'))
Now I get the total summed points for each person correctly.

Related Links

Cookies causing django crash when upgrading from 1.3 to 1.4
getting distinct values from inputs that share the same name
Celery with Django and MongoDB (mongoengine)
How to delete a lot of objects from database
How to use image field in Django unit test
Django and radio buttons
django i18n translation not working on nginx but works on runserver
Django Form won't render
Django: Permissions granted to auth user but not there in view / template?
How do I reset the django cache for static file names?
Apache HTTP Basic Auth - 401 error after supplying username and password
Unable to load require.config dependency when using with Django
How i can show list of objects that have no related object?
Using mixins in class based view - Django
Can I specify a multiline context variable/parameter when {% include %}ing a template?
Django admin panel throws 404 when passing any url parameters

Categories

HOME
grok
scikit-learn
composite-primary-key
solver
sublimetext
nsbundle
x264
pkcs#11
recordrtc
okhttp
drop-down-menu
distribution
github-enterprise
libusb
mvc-mini-profiler
aws-sdk
template-engine
git-extensions
google-container-registry
cfml
solrcloud
googlebot
systemtime
angular-routing
visual-studio-extensions
joptionpane
pimcore
gmock
xcode-ui-testing
jquery-multiselect
outliers
underscore.js
user-defined-functions
iis-express
vex
finagle
magento-1.8
constexpr
event-flow
dst
android-sugarorm
keras-layer
indy10
pdw-file-browser
apic
zoomify
release
z-notation
protein-database
jvmti
magento-1.9.3
dbnull
plr
logback-groovy
stylecop
x++
magento-2.0.7
remedy
video-conferencing
stdmap
context-sensitive-grammar
skin
lsyncd
flyout
d3-force-directed
grails-plugin
xcode-server
envi
dotnetnuke-7
watch-os-2
xdoclet
nss
kiosk
cycle2
concurrentmodification
dynamics-nav-2016
zen-cart
canalyzer
cwrsync
node-serialport
entity-framework-4.3
mruby
addressing-mode
jqmodal
mintty
xml-dsig
android-framework
renderer
gae-quotas
ora-06550
mt4j
inputmismatchexception
cmmi
scmmanager
insert-id
stacky
virtual-pc
nokia-s40
vfs
paginator
boost-signals
kqueue
isa-swizzling
cross-database
update-statement
massive
timthumb
socketserver
ruby-1.8
jqueryform
opml
tabpanel
httpcookie

Resources

Encrypt Message