Questions tagged [django-annotate]
297 questions
1
vote
1 answer
Why annotate dupliquates my queryset objects?
Whenever I use Annotate with Case/When logic to return a boolean, the resulting queryset is almost twice as long.
Model :
class Message(models.Model):
readers = models.ManyToManyField(Compte, related_name='messages')
Message.objects.count() //…

Benoit CHAUVET
- 11
- 2
1
vote
1 answer
Edit result of annotate in a Django queryset
I have a queryset
queryset = BigTable.objects.values('field1__code', 'field2__code').annotate(Sum('field3'))
the resulting value field3__sum = "1234567" is an integer and is very long for my template.
How can I divide it by 1000 (for example) and…

cegthgtlhj
- 191
- 1
- 8
1
vote
1 answer
How do i use annotate to group the child models by the parent models?
I have 3 models. PeerReview Answer and Statement. A Peer review has many answers, Each answer belongs to a Statement (or question if you will)
What I want is to get all answers from all PeerReviews grouped by their statements. So the end result…

FAM_Maurice
- 363
- 3
- 10
1
vote
1 answer
annotate sum of annotated values in related model
I have these models:
class Product(..):
category = ForeignKey('InputCategory...
class InputCategory(MPTTModel):
route_to = ForeignKey('RoutingCategory...
class RoutingCategory(MPTTModel):
pass
So InputCategory has many Products and…

Milano
- 18,048
- 37
- 153
- 353
1
vote
2 answers
How to compute Sum of an aggregate with Django?
I'm currently trying to compute the score of a Survey in SQL side only to be able to order survey by their scores.
My current logic is:
Compute the real coefficient of my Answer
Make the sum of that coeficient for my Question (which can have…

Chr0nos
- 67
- 1
- 9
1
vote
4 answers
Django: remove Decimal prefix from queryset annotated field, when requesting values
to be brief, i have this queryset:
monthly_revenue = list(Booking.objects.annotate(month=Month('created_at'))
.values('month')
.annotate(total=Sum('price'))
…

Simou
- 682
- 9
- 28
1
vote
1 answer
Django annotation resulting in cartesian product
I have a model where a particular class will have multiple child classes. Sample design:
class ParentModel(models.Model):
ParentName = CharField(max_length=50)
class ChildModelA(models.Model):
FK1 = ForeignKey(ParentModel,…

Tom K
- 33
- 6
1
vote
0 answers
Django: Extract doesn't work with DateTimeField
The Extract function of Django ORM returns None after annotation if I use DateTimeField. Why??
models.py
class SimpleModel(models.Model):
date_value = models.DateField()
datetime_value = models.DateTimeField()
settings.py
DATABASES = {
…

JPG
- 82,442
- 19
- 127
- 206
1
vote
1 answer
How to use filter value as variable in django orm
I want to use tmp_id as a value of row id in extra method .
code:
order_obj = table.objects.filter()
.annotate(
tmp_id=F('table2__test_data')
)
.extra(
select={"val":"select id from data where row_id = {{here i want to use
tmp_id}}…

sachin dubey
- 755
- 9
- 28
1
vote
1 answer
Django annotate by sum of two values with multiple relations
I have 4 models:
class App(models.Model):
...
class AppVersion(models.Model):
app = models.ForeignKey(App)
version_code = models.IntegerField()
class Meta:
ordering = ('-version_code',)
...
class Apk(models.Model):
…

artem
- 16,382
- 34
- 113
- 189
1
vote
1 answer
Django ORM sum prices from 3 level nested
I have these models and managers:
class ItemManager(models.Manager):
use_for_related_fields = True
def get_queryset(self):
qs = super().get_queryset()
return qs.annotate(total_price=ExpressionWrapper(
…

Mirza Delic
- 4,119
- 12
- 55
- 86
1
vote
1 answer
How to use annotate in query set to extract a count of type in a location?
I can't seem to properly use annotate to extract the information I need from my models.
I have the follow .model structure:
class ArtistWorkPlaceQuerySet(models.QuerySet):
def with_related(self):
return…

H C
- 1,138
- 4
- 21
- 39
1
vote
1 answer
Rendering results of multiple value Django annotate query with into html table
I am trying to display a summary of statuses by agent. Annotate looks like the way to go, but the data structure doesn't seem to allow me to loop through the objects and populate an html table cleanly. I've tried manipulating the result set…

d-wilcox
- 21
- 3
1
vote
1 answer
How to annotate a queryset with a new field of information from a related model?
I have a model where a Vehicle table has more Wheels tables. I am trying to display in a single field the information of the vehicle and of the first wheel in the related Wheels table. I have seen that the F function might be useful but I cannot…

Andrei
- 59
- 1
- 9
1
vote
2 answers
How to get table alias for inner join in django
I'm using django 2.1, python 3.6 and SQL Server 2012 as backend. I have following models:
class ModelA(models.Model):
name = models.CharField(...)
value = models.PositiveIntegerField(...)
class ModelB(models.Model):
name =…

binaryEcon
- 380
- 4
- 12