Questions tagged [django-aggregation]

django-aggregation refers to an ability to aggregate a collection of objects provided by Django database-abstraction API

django-aggregation refers to an ability to aggregate a collection of objects provided by Django database-abstraction API.

See documentation.

206 questions
176
votes
6 answers

How to filter objects for count annotation in Django?

Consider simple Django models Event and Participant: class Event(models.Model): title = models.CharField(max_length=100) class Participant(models.Model): event = models.ForeignKey(Event, db_index=True) is_paid =…
rudyryk
  • 3,695
  • 2
  • 26
  • 33
120
votes
8 answers

Django: Group by date (day, month, year)

I've got a simple Model like this: class Order(models.Model): created = model.DateTimeField(auto_now_add=True) total = models.IntegerField() # monetary value And I want to output a month-by-month breakdown of: How many sales there were in…
Oli
  • 235,628
  • 64
  • 220
  • 299
93
votes
2 answers

How to group by AND aggregate with Django

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…
Guy Bowden
  • 4,997
  • 5
  • 38
  • 58
76
votes
4 answers

Django order_by() filter with distinct()

How can I make an order_by like this .... p = Product.objects.filter(vendornumber='403516006')\ .order_by('-created').distinct('vendor__name') The problem is that I have multiple vendors with the same name, and I only want the…
pkdkk
  • 3,905
  • 8
  • 44
  • 69
59
votes
7 answers

Django 1.11 Annotating a Subquery Aggregate

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…
Oli
  • 235,628
  • 64
  • 220
  • 299
34
votes
2 answers

Django equivalent of COUNT with GROUP BY

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…
Imran
  • 87,203
  • 23
  • 98
  • 131
28
votes
3 answers

Django - Can you use property as the field in an aggregation function?

I know the short answer because I tried it. Is there any way to accomplish this though (even if only on account of a hack)? class Ticket(models.Model): account = modelfields.AccountField() uuid = models.CharField(max_length=36, unique=True) …
orokusaki
  • 55,146
  • 59
  • 179
  • 257
23
votes
2 answers

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

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…
Jake
  • 12,713
  • 18
  • 66
  • 96
22
votes
5 answers

Get minimum value field name using aggregation in django

I have a model with some fields like below class Choclate(models.Model): name = models.CharField(max_length=256) price = models.IntegerField() So i want to get the field name that has the lowest price value, so in order to get the lowest…
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
19
votes
4 answers

Django Activity Feed (Feedly Integration?)

I have built a simple Django photo app. Users can upload photos, follow other users and like photos. To handle relationships amongst the users (following & unfollowing) I use a package called django-relationships by coleifer. It's a great package…
deadlock
  • 7,048
  • 14
  • 67
  • 115
17
votes
2 answers

Annotating SUM aggregation function leading to 'None' value in Django

Doing my first real Django project, and need guidance. Background: My project is a reddit clone. Users submit links+text. Visitors upvote or downvote. There's a social_ranking algo, runs every ~2 mins as a background script, reranks all the…
Hassan Baig
  • 15,055
  • 27
  • 102
  • 205
15
votes
5 answers

Django GROUP BY strftime date format

I would like to do a SUM on rows in a database and group by date. I am trying to run this SQL query using Django aggregates and annotations: select strftime('%m/%d/%Y', time_stamp) as the_date, sum(numbers_data) from my_model group by…
Andrew C
  • 1,036
  • 2
  • 9
  • 19
15
votes
1 answer

Django - 'WhereNode' object has no attribute 'output_field' error

I am trying to query and annotate some data from my models: class Feed(models.Model): # Feed of content user = models.ForeignKey(User, on_delete=models.CASCADE) class Piece(models.Model): # Piece of content (video or playlist) …
14
votes
1 answer

Exclude null values from Django's ArrayAgg

I'm using Django's postgres-specific ArrayAgg aggregator. It works fine but when the list is empty I get [None] instead of []. Is there any way to filter these null values out? I've tried to pass a filter argument to ArrayAgg but it didn't work.…
13
votes
2 answers

Django conditional annotation

I'm surprised that this question apparently doesn't yet exist. If it does, please help me find it. I want to use annotate (Count) and order_by, but I don't want to count every instance of a related object, only those that meet a certain…
jMyles
  • 11,772
  • 6
  • 42
  • 56
1
2 3
13 14