Questions tagged [django-annotate]
297 questions
0
votes
0 answers
Django subsequent StringAgg with grouping
I'm using Django with postgresql. I have 3 tables with m2m relations: movies, genres and persons. Each person can have it's own role as actor, writer, or director.
title |genre_name|role |name …

Max Rukhlov
- 331
- 3
- 12
0
votes
2 answers
Django Nested Annotate
I have 3 models
class QuestionsModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
Question = models.CharField(max_length=200)
class AnswersModel(models.Model):
Question = models.ForeignKey(QuestionsModel,…

Bosoud
- 158
- 4
- 24
0
votes
1 answer
Django: Make a Queryset with annotate using an object's property
I have a queryset in my Django views.py. I'm using annotate to add a promo property to each object based on each object's id. However this is not working:
bytes = Byte.objects.filter(
published=True
).annotate(
# add path to…

NewToJS
- 2,011
- 4
- 35
- 62
0
votes
1 answer
Django: multply, sum and groupby using two models
Good day,
I am trying to build a system that creates an order and allows a user to add pieces. Here are the two models:
class Order(models.Model):
order_slug = models.SlugField(unique=True, blank=True)
machine = models.ForeignKey(Machine,…

Sents
- 21
- 4
0
votes
3 answers
How to find the highest value of a ForeignKey field for a specific user in Django
I am building an app that allows users to record their workouts. First they will create an exercise, (e.g Bench Press), and then they will complete a form to show how much weight they were able to lift for that specific exercise. Their results will…

sgt_pepper85
- 421
- 4
- 14
0
votes
1 answer
Django Counting related object with a certain condition
I have this model classes in my django app:
class Ad(models.Model):
...
class Click:
time = models.DateTimeField(auto_now_add=True)
ip = models.GenericIPAddressField()
ad = models.ForeignKey(
to=Ad,
…

Ashkan Khademian
- 307
- 3
- 12
0
votes
1 answer
Aggregate annotated field from Subquery in Django
I'm trying to implement a subquery with Django ORM, but I can't find a working solution.
The SQL query that I would need to reverse-engineer is:
select t1.location, sum(t1.value_relative::numeric) as total
from (
select
…

Stefano Luoni
- 81
- 1
- 8
0
votes
0 answers
Django annotate with 'field' of 'related_name' of 'related_name'
I have three models : Domain Topic Post
Topic has a foreign key to Domain and Post has a foreign key to Topic.
Post has a field updated_on.
I want to annotate last_updated field in Domain queryset which would contain the latest post object from Post…

Yusuf
- 321
- 1
- 7
- 19
0
votes
0 answers
Django and aggregating grand-child records
I am new to Django but have been around RdB for a while. I am finally getting the hang of model-view-template. I am struggling a little on "aggregate" and "annotate" especially when my model has grand-child records and I want aggregate.
I use Django…

Matt Revenaugh
- 11
- 2
0
votes
1 answer
How to group all the related data and return as a values dict in django?
I have 3 models:
class City(models.Model):
name = models.CharField(max_length=150)
class Person(models.Model):
name = models.CharField(max_length=150)
city = models.ForeignKey(
City, related_name="persons",…

Alok Ramteke
- 123
- 4
- 11
0
votes
1 answer
Dynamic views in Django ORM - best approach?
Some background
I am considering rebuilding an existing Laravel website with Django. It's a website that allows sharing benchmark data from drone/UAV propulsion components. Some benchmarks are done while testing multiple motors and propellers at the…

theKnack
- 1
- 3
0
votes
1 answer
Get the value of name key from the list of dictionaries return by the annotate and then sort by name?
My Code
def get_areas(self, obj):
text = []
if obj.areas and not obj.areas == []:
print(json.loads(obj.area_count))
print(type(obj.area_count))
return json.loads(obj.area_count)
return…

Chetan Vashisth
- 438
- 4
- 14
0
votes
2 answers
How to annotate median value to django queryset
I have 2 models Puzzle and Play. for each play I have a rating.
I would like to annotate to a Puzzle queryset the median rating value for all corresponding plays.
class Puzzle(models.Model):
name = models.CharField(max_length=255)
class…

Skratt
- 289
- 4
- 13
0
votes
0 answers
How to optimize annotate in django query?
This code is taking 55 seconds to run, is there any way to optimize this? It is unfeasible
job_list = Job.objects.filter(Q(hiring_manager=emp) | Q( followers=emp) | Q(conductors=emp)).values('pk')
_candidates = Candidate.objects.filter(…

GustavoNogueira
- 389
- 1
- 3
- 16
0
votes
2 answers
Django Subquery Expression contains mixed types. You must set output_field
I am trying to add sum for credits that are taxable my models.py. If I calculate the balance without the taxable_credits it works. The minute I add the taxable_credits into the mix I get the error.
class Account(models.Model):
name =…

Luis Berrocal
- 316
- 4
- 12