Questions tagged [django-annotate]
297 questions
7
votes
1 answer
Formatting dates for annotating count in Django + Python 3
I'm currently trying to annotate and count some dates, based on the number of times they appear.
visits = Subs.objects.filter(camp=campdata, timestamp__lte=datetime.datetime.today(),…

Ronald Langeveld
- 694
- 1
- 8
- 23
7
votes
2 answers
django annotate - conditional count
I have a model called 'StoreItem' and a model named 'QuoteItem'. A QuoteItem points on a StoreItem.
I'm trying to annotate a counter of how many quote items point on store items, but with conditions to apply on the quote items.
I tried something…

Ronen Ness
- 9,923
- 4
- 33
- 50
6
votes
3 answers
django annotate models with an aggregate value based on query
Let's say I have the following model structure:
Parent():
Child():
parent = ForeignKey(Parent)
GrandChild():
child = ForeignKey(Child)
state = BooleanField()
num = FloatField()
I'm trying to from the Parent ViewSet, recover the following:
The…

Eric
- 601
- 7
- 22
6
votes
4 answers
How to annotate a difference of datetime in days
I have a Booking model that has start and end datetime fields. I want to know how many days a booking covers. I can do this in Python but I need this value for further annotations.
Here's what I've tried:
In [1]:…

Oli
- 235,628
- 64
- 220
- 299
5
votes
0 answers
Annotation inside an annotation in Django Subquery?
I've got a few models and am trying to speed up the page where I list out users.
The issue is that I was leveraging model methods to display some of the data - but when I listed the Users out it was hitting the DB multiple times per User which ended…

Hanny
- 580
- 3
- 16
- 44
5
votes
1 answer
Django annotation has side effect on another annotation
I'm running into an issue where adding an annotation to a QuerySet changes the results of previous annotations.
Here is the (simplified) setup:
class Player(models.Model):
name = models.CharField()
class Unit(models.Model):
player =…

abey
- 593
- 10
- 26
5
votes
2 answers
Django - annotate with multiple Count
I have a model called Post which has two fields upvotes and downvotes. Now, upvotes, downvotes are ManyToManyField to a Profile. This is the model:
class Post(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
…

Sreekar Mouli
- 1,313
- 5
- 25
- 49
5
votes
1 answer
Django annotate with conditional expression, how to apply distinct()
I have a little bit complex query and I use annotate with a conditional expression, the code is below:
from django.db.models import Q, F, When, CharField, Value, Case
from django.contrib.auth.models import User
ATT_PREFIX = 'f'
def…

Goran
- 6,644
- 11
- 34
- 54
5
votes
2 answers
GeoDjango: Distance Object is not serializable
I am just learning the geo-django. I can find out the distance of all places from a point. But when I use .values method to the annotated distance field, I am getting
TypeError: Object of type 'Distance' is not JSON serializable
Here is my code…

Anonymous Coder
- 119
- 5
4
votes
2 answers
Django annotate returning duplicate entries
I'm annotating a queryset like so:
class ItemQuerySet(models.QuerySet):
def annotate_subitem_stats(self):
return self.annotate(
count_subitems=Count('subitems'),
has_sent_subitems=Case(
…

keyvanm
- 157
- 9
4
votes
1 answer
Django: Annotate list of related fields
I have an Company and User models with a related model CompanyRecruiter:
class CompanyRecruiter(models.Model):
organization = models.ForeignKey(Company, related_name="company_recruiters")
recruiter = models.ForeignKey(User,…

Martin Faucheux
- 884
- 9
- 26
4
votes
2 answers
Django: Problem in doing complex annotation and aggregation
This is model:
class Purchase(models.Model):
date = models.DateField(default=datetime.date.today,blank=False, null=True)
total_purchase = models.DecimalField(max_digits=10,decimal_places=2,blank=True, null=True)
I want to perform…

Niladry Kar
- 1,163
- 4
- 20
- 50
4
votes
1 answer
Multiple annotate with sum and display data in admin - Django
I'm new to both Django and Python. Currently I'm trying the Django Admin by doing.
I've three models for a Django app, which are GoodsItem, SoldGoodsItem and FinishedGoodsItem. The models.py is:
from django.db import models
class…

rmalviya
- 1,847
- 12
- 39
4
votes
1 answer
Django: get duplicates based on annotation
I want to get all duplicates based on a case insensitive field value.
Basically to rewrite this SQL query
SELECT count(*), lower(name)
FROM manufacturer
GROUP BY lower(name)
HAVING count(*) > 1;
with Django ORM. I was hoping something like this…

Dušan Maďar
- 9,269
- 5
- 49
- 64
4
votes
1 answer
Why do I get different results if I use first() vs last() on a QuerySet with the length of 1
while writing a testcase for a method I discovered that I get different results if I use my_queryset.first().my_annotated_value than when I use my_queryset.last().my_annotated_value although my_queryset.count() returns 1.
Here is the relevant code…

jimfawkes
- 357
- 2
- 12