django-aggregation

Using .aggregate() on a value introduced using .extra(select={…}) in a Django Query?

一世执手 提交于 2019-11-27 07:06:57
I'm trying to get the count of the number of times a player played each week like this: player.game_objects.extra( select={'week': 'WEEK(`games_game`.`date`)'} ).aggregate(count=Count('week')) But Django complains that FieldError: Cannot resolve keyword 'week' into field. Choices are: <lists model fields> I can do it in raw SQL like this SELECT WEEK(date) as week, COUNT(WEEK(date)) as count FROM games_game WHERE player_id = 3 GROUP BY week Is there a good way to do this without executing raw SQL in Django? You could use a custom aggregate function to produce your query: WEEK_FUNC = 'STRFTIME("

Django equivalent of COUNT with GROUP BY

我怕爱的太早我们不能终老 提交于 2019-11-27 05:13:29
问题 I know Django 1.1 has some new aggregation methods. However I couldn't figure out equivalent of the following query: SELECT player_type, COUNT(*) FROM players GROUP BY player_type; Is it possible with Django 1.1's Model Query API or should I just use plain SQL? 回答1: If you are using Django 1.1 beta (trunk): Player.objects.values('player_type').order_by().annotate(Count('player_type')) values('player_type') - for inclusion only player_type field into GROUP BY clause. order_by() - for exclusion

Django 1.11 Annotating a Subquery Aggregate

巧了我就是萌 提交于 2019-11-27 03:12:25
This is a bleeding-edge feature that I'm currently skewered upon and quickly bleeding out. I want to annotate a subquery-aggregate onto an existing queryset. Doing this before 1.11 either meant custom SQL or hammering the database. Here's the documentation for this , and the example from it: from django.db.models import OuterRef, Subquery, Sum comments = Comment.objects.filter(post=OuterRef('pk')).values('post') total_comments = comments.annotate(total=Sum('length')).values('total') Post.objects.filter(length__gt=Subquery(total_comments)) They're annotating on the aggregate, which seems weird

How to group by AND aggregate with Django

血红的双手。 提交于 2019-11-26 17:22:25
I have a fairly simple query I'd like to make via the ORM, but can't figure that out.. I have three models: Location (a place), Attribute (an attribute a place might have), and Rating (a M2M 'through' model that also contains a score field) I want to pick some important attributes and be able to rank my locations by those attributes - i.e. higher total score over all selected attributes = better. I can use the following SQL to get what I want: select location_id, sum(score) from locations_rating where attribute_id in (1,2,3) group by location_id order by sum desc; which returns location_id |

Using .aggregate() on a value introduced using .extra(select={…}) in a Django Query?

北城余情 提交于 2019-11-26 13:03:39
问题 I\'m trying to get the count of the number of times a player played each week like this: player.game_objects.extra( select={\'week\': \'WEEK(`games_game`.`date`)\'} ).aggregate(count=Count(\'week\')) But Django complains that FieldError: Cannot resolve keyword \'week\' into field. Choices are: <lists model fields> I can do it in raw SQL like this SELECT WEEK(date) as week, COUNT(WEEK(date)) as count FROM games_game WHERE player_id = 3 GROUP BY week Is there a good way to do this without

How to group by AND aggregate with Django

那年仲夏 提交于 2019-11-26 06:05:49
问题 I have a fairly simple query I\'d like to make via the ORM, but can\'t figure that out.. I have three models: Location (a place), Attribute (an attribute a place might have), and Rating (a M2M \'through\' model that also contains a score field) I want to pick some important attributes and be able to rank my locations by those attributes - i.e. higher total score over all selected attributes = better. I can use the following SQL to get what I want: select location_id, sum(score) from locations