Questions tagged [django-annotate]
297 questions
1
vote
1 answer
PostgreSQL Programming error in Django Query
I have a query as below which returns the grade of all students of a specific class in a specific term(semester) in a specific session(academic year):
grades = Grade.objects.filter(term='First',…

Abdoul
- 125
- 3
- 8
1
vote
1 answer
Django multiple annotations returns wrong results
I am trying to create an annotation in a django queryset. The annotation is a Count of reverse foreign key based on a condition. The issue I am having is that when I do the count for one reverse foreign key with a condition I get the right data, but…

nael
- 1,441
- 19
- 36
1
vote
3 answers
Django annotate several same objects in QuerySet by different related object
I got:
# models
class Building(models.Model):
...
class Flat(models.Model):
building = models.ForeignKey(Building)
class Profile(models.Model):
flats = models.ManyToManyField(Flat)
# logic
building =…

valex
- 5,163
- 2
- 33
- 40
1
vote
1 answer
Django sum on query set
I have a queryset in django with condition as follows:
query_set = MyModel.objects.annotate(TWWByRuns=(
Case(
When(Q(toss_winner=F('winner')) & Q(win_by_runs=0), then=1),
output_field=FloatField(), default=0)
…

Lax_Sam
- 1,099
- 2
- 14
- 32
1
vote
1 answer
Count a Count with Django Aggregation through multiple Many to Many fields
Given a Django model with two M2M fields:
class Book(models.Model):
name = models.CharField(max_length=300)
authors = models.ManyToManyField(Author)
publishers = models.ManyToManyField(Publisher)
And starting from a queryset of…

43Tesseracts
- 4,617
- 8
- 48
- 94
1
vote
1 answer
Django Annotate - get list of distinct field values
I have 3 models - Book, Page and Language.
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
class Page(models.Model):
image = models.ImageField(upload_to='Uploaded_images')
…

rahul
- 366
- 7
- 21
1
vote
1 answer
Django - Annotate with count across ManytoMany relationships
I am not able to find the way to annotate a queryset with a count of how many times an element is used in a many-to-many relationship.
class Profile(models.Model):
[...]
# Profile can have multiple roles
roles =…

shitzuu
- 119
- 2
- 9
1
vote
1 answer
Use annotate and Case with different field types in Django
I have a model with those field:
class ModelA(models.Model):
parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.PROTECT, related_name='child')
number = models.PositiveIntegerField()
text_code =…

Tiago Gomes
- 357
- 4
- 12
1
vote
0 answers
Django create a subtable
I have three models as below:
class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
class PersonSession(models.Model):
start_time = models.DateTimeField(auto_now_add=True)
…

motam
- 677
- 1
- 6
- 24
1
vote
0 answers
Django annotations using F() expressions with DateTimeFields
I want to write a custom QuerySet and Manager for a model that has a DateTimeField("installed_date" field) and an IntegerField ("expiration_days"), and I want to filter objects based on the rule " 'installed_date' plus 'expiration_days' is prior to…

naccode
- 510
- 1
- 8
- 18
1
vote
0 answers
How to access an entire table row based on annotation with Max()
I have a model that stores the cover images for different magazines (publications).
In a simplified version it looks like this
class Publication(models.Model):
name = models.CharField(max_length=100)
website =…

Philip B.
- 637
- 6
- 12
1
vote
1 answer
django annotate weird behavavior (group by model.id)
In my DRF API, I have a view like this
class ActivityAPI(viewsets.ModelViewSet):
authentication_classes = (SessionAuthentication, TokenAuthentication)
serializer_class = ActivitySerializer
queryset =…

JPG
- 82,442
- 19
- 127
- 206
1
vote
1 answer
Django annotation per day time range
I am using the following query to get a sum per day:
orders = OrderM.objects.filter(user=request.user) \
.annotate(day=TruncDay('datetime_added')) \
.values('day') \
…

Vassilis
- 2,878
- 1
- 28
- 43
1
vote
1 answer
django annotating value with model field name
I have the following model
class Status(object):
FIRST_STATUS = 'FS'
SECOND_STATUS = 'SS'
CHOICES = ((FIRST_STATUS, 'First Status'), (SECOND_STATUS, 'Second Status')
class MyModel(models.Model):
status =…

Mr T.
- 4,278
- 9
- 44
- 61
1
vote
1 answer
Django query to Sum the lengths of ArrayFields
I have this model:
class Interaction(models.Model):
user = models.ForeignKey(User)
codes = ArrayField(models.CharField(choices=CODE_CHOICES))
and I'm trying to figure out how to do the equivalent of this SQL query in Django:
select user_id,…

TAH
- 1,658
- 1
- 19
- 37