Questions tagged [django-annotate]
297 questions
2
votes
1 answer
Return annotated queryset against graphql queries on django graphene
class Project(models.Model):
name = models.CharField(max_length=189)
class Customer(models.Model):
name = models.CharField(max_length=189)
is_deleted = models.BooleanField(default=False)
project = models.ForeignKey(Project,…

Mehran
- 1,264
- 10
- 27
2
votes
0 answers
Extra conditional annotation breaks previous annotation
I have the following models in a game app I'm working on:-
class Player(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='players')
class Game(models.Model):
players = models.ManyToManyField(Player,…

bodger
- 1,112
- 6
- 24
2
votes
2 answers
Django annotate Avg on model field
I have these models:
class Agency(models.Model):
pass
class User(models.Model):
agency = models.ForeignKey(Agency)
class Feedback(models.Model):
rating = models.DecimalField()
user = models.ForeignKey(User)
and I want to…

Ross Lote
- 814
- 1
- 8
- 19
2
votes
2 answers
Annotating related and multi-filtered objects in Django
I have a queryset of profiles:
Model:
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, unique=True)
...
View:
Profile.objects.select_related('user')
Each user/profile can register…

43Tesseracts
- 4,617
- 8
- 48
- 94
1
vote
0 answers
Django: SUM of COUNT in annotations
I have the following simple models:
class Member(models.Model):
name = models.CharField(max_length=100)
class Booking(models.Model):
date = models.DateField(default=now)
price = models.DecimalField(max_digits=7, decimal_places=2)
…

SimpleFlow
- 31
- 3
1
vote
1 answer
Get average of annotated fields in Django (postgres)
Consider the following models:
class Employee(models.Model):
name = models.Charfield(max_length=100)
class Evaluation(models.Model):
employee = models.ForeignKeyField(Employee, on_delete=models.CASCADE)
question1 =…

DrS
- 342
- 1
- 3
- 15
1
vote
1 answer
Annotating Many2Many link works but only on SQLite (testing) not MariaDB (Production)
I am trying to annotate a model that includes a many2many field:
class Link(models.Model):
products = models.ManyToManyField(Product, related_name = "%(class)s_name", related_query_name = "product_link_qs", blank = True)
position =…

xtlc
- 1,070
- 1
- 15
- 41
1
vote
1 answer
Django Queryset: group by two field values
I have a model like this
Class ExchangeMarket(models.Model):
base_currency = models.ForeignKey(Currency)
quote_currency = models.ForeignKey(Currency)
... various other fields
And some entries like
base: EUR, quote: USD ...
base: EUR, quote: USD…

ElRey777
- 191
- 1
- 12
1
vote
1 answer
How to print object's values with "annotate()" and "for loop" in ascending order?
I have Category and Product models below. *I use Django 3.2.16:
# "models.py"
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=20)
class Product(models.Model):
category =…

Super Kai - Kazuya Ito
- 22,221
- 10
- 124
- 129
1
vote
1 answer
Django annotate field value from external dictionary
Lets say I have a following dict:
schools_dict = {
'1': {'points': 10},
'2': {'points': 14},
'3': {'points': 5},
}
And how can I put these values into my queryset using annotate?
I would like to do smth like this, but its not…

mecdeality
- 405
- 4
- 13
1
vote
1 answer
django annotate with dynamic "then" value
So i have this dictionary of months below:
MONTHS = {
1: "Janeiro",
2: "Fevereiro",
3: "Março",
4: "Abril",
5: "Maio",
6: "Junho",
7: "Julho",
8: "Agosto",
9: "Setembro",
10: "Outubro",
11: "Novembro",
…

Ives Furtado
- 50
- 5
1
vote
1 answer
How to cast django.db.models F to a String?
Note: working on expensive corporate legacy code and models that are not allowed to be changed
What I'm trying to do:
Combine two fields into one as a DateTime field.
Model.py:
from django.db import models
class DateThing(models.Model):
date…

Rio Weber
- 2,757
- 1
- 20
- 28
1
vote
2 answers
How to Django queryset annotate True when all BooleanField of related objects are True else False?
I have a model who looks like this :
class Test(models.Model):
user = models.ForeignKey('users.CustomUser', models.CASCADE)
name = models.CharField(max_length=64)
class TestVersion(models.Model):
test = models.ForeignKey('Test',…

Florian
- 61
- 1
- 11
1
vote
2 answers
Annotate results from related model method onto model Queryset?
I'm trying to figure out the best / most efficient way to get the 'progress' of a Summary object. A Summary object has X Grade objects - a Grade object is_complete when it has a Level chosen and has 1 or more related Evidence objects.
I am trying to…

UpDawg
- 51
- 6
1
vote
1 answer
Is it possible to use aggregate on a queryset annotation?
I am using annotate on a django queryset:
class MyModel(models.Model):
my_m2m = models.ManytoManyField()
my_qs = MyModel.objects.all().annotate(total_m2m=Count('my_m2m'))
This yields the desired result. E.g:
>>> my_qs[0].total_m2m
>>> 3
How do…

alias51
- 8,178
- 22
- 94
- 166