Questions tagged [django-annotate]
297 questions
1
vote
1 answer
How to use percentage sign in annotate key
I'm trying to annotate a result column name with string that includes a percentage sign. This is the simplified code which can be run on any model basically:
from django.db.models import F
annotates = {'TEST %':…

Igor Jerosimić
- 13,621
- 6
- 44
- 53
1
vote
1 answer
Django annotated queryset with latest values for some fields
I have a model which store some marketing performances:
class DailyPerformance(models.Model):
class Meta:
unique_together = ('route_hash_value', 'datetime_tz')
ordering = ('-datetime_tz',)
route_hash_value =…

Tommaso Castellani
- 31
- 3
1
vote
0 answers
Calculate time difference (Subtract the time range 10PM to 9AM) between two datetime fields using django annotate?
I have to calculate the time difference between to DateTime fields, also keeping in mind to subtract the time frame 10 PM-9 AM for each transaction if it lies in.
The following code snippet saves the duration between those two fields but I'm unable…

Bhirendra Kumar Sahu
- 101
- 1
- 9
1
vote
0 answers
Annotate a datetime field from another date time and duration in minutes
I want to annotate a datetime field to a queryset using timedelta and F objects
Example:
Model.objects.annotate(end_time=F('start_time')+timedelta(minutes=F('meeting_duration')))
Here I have a field "start_time" and "meeting_duration" in my table…

Mohit Harshan
- 1,916
- 1
- 18
- 41
1
vote
1 answer
How to order by a non-model remote field like this?
I have a table like this:
class SystemPushLog(models.Model):
...
user_id = models.IntegerField(null=True)
As u can see in the model above, user_id is a IntegerField, not a foreign key. Cuz user infos in out system are retrieved from gRPC…

Gorgine
- 310
- 2
- 11
1
vote
0 answers
Django query More than one row returned by a subquery used as an expression
I have a pretty simple query and I need to add one field to get an average price grouped by benefit - I tried with ArrayAgg but is not working.
properties = (
House.objects.filter(
# a lot of filters
).annotate(
…

Janosz
- 21
- 2
1
vote
0 answers
Django annotate NumericRange - AttributeError: 'NumericRange' object has no attribute 'resolve_expression'
I have DRF custom filter and I'm struggling with building query for NumericRange.
I need to create NumericRange(or other range for integers) with two calculated values and then use that field to filter it using __overlap.
def filter_range(queryset,…

Janosz
- 21
- 2
1
vote
1 answer
Django QuerySets - how to annotate off one field but return a different field?
Looking at the example given in the docs: https://docs.djangoproject.com/en/3.0/topics/db/aggregation/
Relevant Code to my question (from docs):
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
…

Johnny
- 103
- 1
- 13
1
vote
0 answers
Use annotate to sum only the entries with a distinct field (field not used in values and sum functions)
Model
I have a model in Django, which has several fields, but, for the purpose of this question, it can be summarized with the following fields:
from django.db import models
from django.core.validators import MaxValueValidator,…

David Duran
- 1,786
- 1
- 25
- 36
1
vote
1 answer
Django annotate queryset with counting its members by condition
I have a model:
class Event(models.Model):
start_date = DateTimeField()
end_date = DateTimeField()
I want to count all events which are of the same day and include this count as annotation field daily_events to the each member of queryset.…

goodgrief
- 378
- 1
- 8
- 23
1
vote
2 answers
django model method to return queryset with annotate
i am trying to define a field which being calculated based on user_type, I did the following method but its not working, if anyone can advise me to the correct approach.
so what i am trying to achieve here is to construct a method such as…

Ahmed Al-Haffar
- 520
- 4
- 17
1
vote
1 answer
get count of a Subquery
I want to annotate a count field in my queryset with a subquery filter:
My code:
module_attempts = Subquery(
ModuleProgressAttempt.objects.filter(
module_progress__module__id=OuterRef("pk")
).only("pk")
)
real_instances =…

Mohit Harshan
- 1,916
- 1
- 18
- 41
1
vote
1 answer
Django Annotate With Sum And Min/Max At The Same Time In Search Form
I have a page of courses which are search results of a course search form. In the current code below, it currently allows a user to type in the min and max views and return courses that contain that Course "views" criteria.
It incorrectly does this…

atavisti
- 13
- 6
1
vote
0 answers
Django model: fetch last ForeignKey in one query
In Django models, I have two database models like:
A(models.Model):
....
b = models.ForeignKey(B, related_name='my_a')
B(models.Model):
....
Now, I would like to fetch all B, but have last A be fetched with the same query.
so…

Marko Zadravec
- 8,298
- 10
- 55
- 97
1
vote
1 answer
Django annotate with related_name from A->C where A->B and B->C where '->' is reverse relationship
I searched a lot on internet but could not find a similar question.
I have 3 models : Domain, Topic, Post
Each Domain can have many Topics and each Topic can have many Posts.
Topic has a foreign key to Domain and Post has a foreign key to Topic.
So…

Yusuf
- 321
- 1
- 7
- 19