Discussion:
Annotate giving back individual queries rather than results
(too old to reply)
Coder Dude
2018-12-02 13:14:21 UTC
Permalink
Here are my models:
from django.contrib.auth.models import User as DefaultUser
class Submission(models.Model):
user = models.ForeignKey(to=DefaultUser,
on_delete=models.CASCADE
)
total_score = models.DecimalField()


The query that I am trying is :

*Submission*.*objects*.values('user').annotate(Count('id'))


I want to get the count of Submissions for each user

But this returns me set of individual submission with count as 1


*Output:*

{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 2, 'id__count': 1}
{'user': 2, 'id__count': 1}
{'user': 3, 'id__count': 1}


Whereas the output that I want is:

{'user': 1, 'id__count': 4}
{'user': 2, 'id__count': 2}
{'user': 3, 'id__count': 1}


What am I doing wrong?

Please help
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5066d27d-632d-4c01-98d3-2114c8043e1d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Coder Dude
2018-12-02 13:51:16 UTC
Permalink
Solved It.
It seems as of django 2.1, we need to add another parameter order_by

*Submission*.*objects*.values('user').annotate(Count('id')).order_by('user')


*documentation <https://docs.djangoproject.com/en/dev/topics/db/aggregation/#interaction-with-default-ordering-or-order-by>*


*Thanks*
Post by Coder Dude
from django.contrib.auth.models import User as DefaultUser
user = models.ForeignKey(to=DefaultUser,
on_delete=models.CASCADE
)
total_score = models.DecimalField()
*Submission*.*objects*.values('user').annotate(Count('id'))
I want to get the count of Submissions for each user
But this returns me set of individual submission with count as 1
*Output:*
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 2, 'id__count': 1}
{'user': 2, 'id__count': 1}
{'user': 3, 'id__count': 1}
{'user': 1, 'id__count': 4}
{'user': 2, 'id__count': 2}
{'user': 3, 'id__count': 1}
What am I doing wrong?
Please help
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/add4bede-4a98-4124-a2d9-eecfd393763a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Simon Charette
2018-12-02 20:52:33 UTC
Permalink
Hey there,

I just wanted to clarify that the behavior of including the Meta.ordering
fields in aggregation grouping been around for a while and Django 2.2
deprecates it to deal with the exact type of confusion you experienced here.

Best,
Simon
Post by Coder Dude
Solved It.
It seems as of django 2.1, we need to add another parameter order_by
*Submission*.*objects*.values('user').annotate(Count('id')).order_by('user')
*documentation <https://docs.djangoproject.com/en/dev/topics/db/aggregation/#interaction-with-default-ordering-or-order-by>*
*Thanks*
Post by Coder Dude
from django.contrib.auth.models import User as DefaultUser
user = models.ForeignKey(to=DefaultUser,
on_delete=models.CASCADE
)
total_score = models.DecimalField()
*Submission*.*objects*.values('user').annotate(Count('id'))
I want to get the count of Submissions for each user
But this returns me set of individual submission with count as 1
*Output:*
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 1, 'id__count': 1}
{'user': 2, 'id__count': 1}
{'user': 2, 'id__count': 1}
{'user': 3, 'id__count': 1}
{'user': 1, 'id__count': 4}
{'user': 2, 'id__count': 2}
{'user': 3, 'id__count': 1}
What am I doing wrong?
Please help
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2ca60905-62e9-4814-9d03-74f227f0604d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...