Questions tagged [django-annotate]

297 questions
4
votes
0 answers

Annotate Django ModelAdmin list without annotating the count query

So for a long time I've been doing annotations in ModelAdmins to add complicated number and related fields to list admin displays. Here's a simple count example. class MyClassAdmin(admin.ModelAdmin): list_display [ .. 'capacity', ..] def…
Oli
  • 235,628
  • 64
  • 220
  • 299
3
votes
0 answers

Django Annotate the Sum of a Foreign Key's Duration Values

I'm trying to get 5 foo objects whose children (bar) have the longest combined duration: foo = Foo.objects.annotate( sum_of_bar_duration=Sum("bar__duration") )\ .order_by('-sum_of_bar_duration')[:5] My Models: class Foo(models.Model): …
newbCoder
  • 31
  • 2
3
votes
2 answers

Django annotate a Boolean if field value is in an external list

Imagine I have this Django model: class Letter(models.Model): name = models.CharField(max_length=1, unique=True) and too this list: vowels = ['a', 'e', 'i', 'o', 'u'] I want to make a query over Letter annotating a boolean field, which will be…
3
votes
0 answers

How to use annotate and KeyTextTransform and KeyTransform to extract first element of nested array?

I am using Django 2.2 and postgres I have a jsonb field called actions And this is test value actions={ "accept": [ { "id": 123, # some random action id "created":…
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
3
votes
1 answer

Annotation with a subquery with multiple result in Django

I use postgresql database in my project and I use below example from django documentation. from django.db.models import OuterRef, Subquery newest =…
3
votes
1 answer

Django filtering a timedelta error: TypeError: expected string or bytes-like object

I have a model Run that contains start_time and end_time timestamps that signify the beginning of data measuring, and the end (we call that a "run"). class Run(models.Model): start_time = models.DateTimeField(db_index=True) end_time =…
3
votes
2 answers

Is there a way to annotate django query with non-expression?

I have use case where I need to get all objects where existing_field is the beginning of some string. some string changes dynamically so I need a smart way to filter out objects. My idea is to create annotated query like…
makozaki
  • 3,772
  • 4
  • 23
  • 47
3
votes
2 answers

How do I make a list of field values on annotated Django queryset results?

This may be out there somewhere, but not quite sure how to ask it. Hopefully it will be a quick one. Say I have a table like the following: name count site Guy Man 2 ABC Guy Man 3 BCD Guy Man 4 CDE Girl…
meesterguyperson
  • 1,726
  • 1
  • 20
  • 31
3
votes
1 answer

Django annotate average query not showing decimal places

I'm working with Django and I'm taking an average for some column values, however the result is rounded up and I'd like to make it one or two decimal places. My goal is to see the result look like 3.5 instead of 4. My model is named Application and…
asdf123
  • 65
  • 1
  • 6
3
votes
1 answer

Django: Annotate sum on variable number of columns

So I've read the annotate columns, and utilizing the F() functions, and I saw this post on how to sum multiple columns. However, I'm working with an EAV style DB, so I'm given a variable list of columns. Consider the example: class…
Ivan Peng
  • 579
  • 5
  • 16
3
votes
2 answers

Django admin custom field fetching result of latest related object by condition

I have three models class Customer(models.Model): created = models.DateTimeField(auto_now_add=True) # some other fields, which don't matter class ActivityStatus(models.Model): created = models.DateTimeField(auto_now_add=True) name =…
3
votes
1 answer

Django annotate with casted variable

i want to achieve something complex with a django queryset. My model looks like this: class Mymodel(models.Model): #Model Variables year = models.IntegerField() month = models.IntegerField() date = models.DateField(_("Date"),…
3
votes
0 answers

Django nested aggregates with group_by

I have a query select avg(total) from (select date, sum(value) as total from table group by some_field, date) as res group by week(date) This is the way I'm getting sum of metrics for each date and show average result grouped by week.…
3
votes
1 answer

django annotate a function that returns the maximum of a field among all objects that has a feature

Suppose that I have this model: class Student(models.Model): class_name = models.CharField() mark = models.IntegerField() And I want to get all the students that have the highest mark in their class. I can get the student who has the…
Navid777
  • 3,591
  • 8
  • 41
  • 69
2
votes
2 answers

Django annotate(),Count()

I get the output like this using annotate() and Count()
Krishna
  • 33
  • 3
1 2
3
19 20