Questions tagged [django-annotate]

297 questions
188
votes
4 answers

aggregate() vs annotate() in Django

Django's QuerySet has two methods, annotate and aggregate. The documentation says that: Unlike aggregate(), annotate() is not a terminal clause. The output of the annotate() clause is a…
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
28
votes
2 answers

How to subtract two annotated columns on Django QuerySets?

I need to be able to sort on the aggregate of two annotated columns So I'd like to do something like this: c = c.annotate(metric=Sum('results__metric')) c = c.annotate(metric_prior=Sum('results__metric_prior')) c =…
James R
  • 4,571
  • 3
  • 30
  • 45
21
votes
2 answers

Django queryset annotate field to be a list/queryset

I'm trying to use django annotation to create queryset field which is a list of values of some related model attribute. queryset = ... qs = queryset.annotate( list_field=SomeAggregateFunction( Case(When(related_model__field="abc"),…
zyks
  • 521
  • 1
  • 4
  • 12
17
votes
3 answers

How to concatenate two model fields in a Django QuerySet?

Consider a table called DataTable. It has two fields: A and B. I want to return all rows from this table, and annotating a field called C which is a concatenation of A and B fields. Here is what I have tried: from django.db.models import CharField,…
yoyoyoyo123
  • 2,362
  • 2
  • 22
  • 36
17
votes
3 answers

Django custom for complex Func (sql function)

In the process of finding a solution for Django ORM order by exact, I created a custom django Func: from django.db.models import Func class Position(Func): function = 'POSITION' template = "%(function)s(LOWER('%(substring)s') in…
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
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.…
12
votes
3 answers

Django conditional Subquery aggregate

An simplified example of my model structure would be class Corporation(models.Model): ... class Division(models.Model): corporation = models.ForeignKey(Corporation) class Department(models.Model): division =…
Eldamir
  • 9,888
  • 6
  • 52
  • 73
12
votes
1 answer

django - annotate() - Sum() of a column with filter on another column

I have the following two models. class Product(models.Model): product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False) manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False) …
art
  • 1,358
  • 1
  • 10
  • 24
12
votes
1 answer

Django's annotate Count with division returns integer instead of float

I have many objects and 3 of them have name='AAA' I group them by 'name' and annotate num in group: my_models = MyModel.objects.order_by('name').values('name').annotate(count=Count('name')) for i in my_models: print(i.count, i.name) I get: 3,…
MaxCore
  • 2,438
  • 4
  • 25
  • 43
9
votes
2 answers

Using Subquery to annotate a Count

Please help me I've been stuck on this for way too long :( What I want to do: I have these two models: class Specialization(models.Model): name = models.CharField("name", max_length=64) class Doctor(models.Model): name =…
9
votes
1 answer

Django annotate data by date for empty result

Suppose I have a object model A, and it has a field called created, which is a datetime type field. If I use annotate to count how many A are created each day, I can use A.objects.annotate(date=Trunc('created', 'day', output_field=DateField())…
Tiancheng Liu
  • 782
  • 2
  • 9
  • 22
8
votes
1 answer

Django annotate count in JSONField with Postgres

Using Django I have a field which is of type JSONField. I am wanting to get a distinct count on a nested key/value in the json. With a normal field you can just do soemthing like the following …
ms18
  • 335
  • 1
  • 3
  • 11
8
votes
1 answer

How to annotate the result of a model method to a django queryset

From the django docs on annotate(): Annotates each object in the QuerySet with the provided list of query expressions. An expression may be a simple value, a reference to a field on the model (or any related models), or... Is it possible to…
43Tesseracts
  • 4,617
  • 8
  • 48
  • 94
1
2 3
19 20