Questions tagged [django-annotate]
297 questions
0
votes
1 answer
Django Subquery returns only the first element
This is a simplified version of the models:
class Toy(models.Model):
#generic fields
class Order(models.Model):
customer = models.ForeignKey(Customer)
class OrderItem(models.Model):
order = models.ForeignKey(Order)
toy =…

panosl
- 1,709
- 2
- 12
- 15
0
votes
2 answers
Django filter on date contained by any date range
I'm trying to filter a model which has a DateField (date) to retrieve a queryset of instances whose date is in any one of a list of DateRanges but I'm struggling to figure out the exact logic I need.
So for example, if I have the following…

bodger
- 1,112
- 6
- 24
0
votes
0 answers
Complex django annotation
I have the following models:-
class Group(models.Model):
name = models.CharField(max_length=200)
class Game(models.Model):
group = models.ForeignKey(Group, related_name='games')
date = models.DateField()
players =…

bodger
- 1,112
- 6
- 24
0
votes
2 answers
Django Model: Find Avg by Grouping from models
I have been trying to find the avg for rating a get queryset.
This is my models.py
class Movie(models.Model):
title = models.CharField(max_length=240)
release = models.DateField(blank=True)
review = models.TextField()
user =…

Gokul
- 68
- 1
- 10
0
votes
1 answer
Django 2.0 Queryset annotate date
Is there a way to pass an F() expression to a function such as dateutil's relativedelta inside a database annotate call.
Given the below scenario where it is intended that InterestLoan.objects.active_loans(start_date='2018-01-01',…

Todd Langford-Archer
- 53
- 1
- 6
0
votes
0 answers
Annotate with Count yields incorrect values with NullBooleanField
I am performing the following query with a few annotations:
(AwardIssueProcess.objects.filter(grant__client=client, completed=completed_status)
.annotate(total_awarded=Sum('award__awardissuedactivity__units_awarded'),
…

Daniel Holmes
- 1,952
- 2
- 17
- 28
0
votes
0 answers
Django ORM queryset takes integer filed as string
results = UmUrl.objects \
.filter(created_at__gte=sdate, created_at__lte=edate) \
.annotate(timeValue=self.get_date_format(param)) \
.values('timeValue') \
.order_by('timeValue') \
…

N'bia
- 27
- 1
- 8
0
votes
1 answer
Combine 2 object of same model Django
Is there any way to combine/merge 2+ objects of the same model to 1 with total values of all field.
I mean like Author.objects.annotate(Sum('book__pages')) but for all fields in model.
1 object - {'user':2, 'events':20, 'time': 233}
2 object -…

Beliaf
- 577
- 2
- 8
- 25
0
votes
0 answers
Django many to many queryset trouble
I have 2 models
class Service(models.Model):
name = models.CharField(max_length=100)
class ServiceType(models.Model):
name = models.CharField(max_length=100)
services = models.ManyToManyField(Service, related_name='service_types')
Now…

Tapo4ek
- 313
- 2
- 7
0
votes
2 answers
Rename nested annotated field via Django ORM
Is it possible to renaming nested fields using for group by clause?
This query:
paymentitem.objects.filter(payment=p).values('item__vat_tax').annotate(base=models.Sum('price')).order_by('item__vat_tax')
returns expected data:

user3437022
- 97
- 1
- 9
0
votes
1 answer
filter dataset not having a value
Suppose i have a schema like below :
Book | Author
-------------
B1 | A1
B1 | A3
B1 | A2
B2 | A5
B2 | A4
B2 | A3
B3 | A5
B3 | A6
B3 | A1
B4 | A1
B4 | A5
B4 | A6
with below model:
class…

NoobEditor
- 15,563
- 19
- 81
- 112
0
votes
0 answers
Django increment Then value in Annotate Count
Let's say I have query set A.
A.annotate(c=Count(Case(When(name='Jack',then=I)
Now, each time A.name == 'Jack', c will be set to I. How can I increment I for each time this happens? So that for the first case, I = 1, for the 2nd I=2 etc.

Mitchell van Zuylen
- 3,905
- 4
- 27
- 64
0
votes
1 answer
Django adding the values of reverse foreign key as field while returning
I have two models. One is Task model and other is reward model.
class Task(models.Model):
assigned_by = models.CharField(max_length=100)
class Reward(models.Model):
task = model.ForeignKey(Task)
Now I want to return a queryset of Task…

gopiariv
- 454
- 7
- 9
0
votes
1 answer
Django Naturaltime is not working in .annotate
Here I just wants to annotate a field on a model that gives human readable format saying how much time elapsed since it's created
My Model is created 30 seconds ago
My Model Description:
from django.db import models
class MyModel(models.Model):
…

Raju Ahmed Shetu
- 134
- 2
- 13
0
votes
1 answer
Django annotate not bringing back distinct
I am trying to query a queryset in Django using annotate to get a calculation for each different 'field2'. The query looks like:
offer_lines.filter('field1'=x).order_by().values('field2').annotate(
total=F('field3') *…

clue3434
- 215
- 1
- 2
- 8