Questions tagged [django-annotate]
297 questions
1
vote
1 answer
Complex aggregation in Django
Using Django rest framework 3.x and Django 1.1.10. I have a model that represents users. When I list all the users by accessing /users/ endpoint in DRF the list has to include some more data that is related to users via another model, called Owner.…

BigWhale
- 23
- 2
- 8
1
vote
0 answers
Django query aggration with on itself join
With the following model, I would like to select root users having the most posts in validated state (in a single query or at least in an optimized way).
In my case root user are not directly authors, I want to aggregate children user authors on…

srjjio
- 930
- 1
- 8
- 16
1
vote
1 answer
Using Django ORM query How to annotate if multiple levels of foreign key hierarchy exists
My Django models looks like
class Parent(models.Model):
name = models.CharField(('name'), max_length=30)
class Child(models.Model):
parent = models.ForeignKey(Parent)
name = models.CharField(('name'), max_length=30)
class…

user2959723
- 651
- 2
- 7
- 13
0
votes
0 answers
Manager's annotations don't show up when traversing ForeignKey
Suppose I have such classes:
class UserManager(Manager):
def get_queryset(self):
return super().get_queryset().annotate(
full_name=Concat(
F('first_name'), Value(' '), F('last_name'),
…

Vitalii Korniichuk
- 15
- 7
0
votes
0 answers
How to set date_hierarchy to an annotation field
I have a Model and its Manager. I created an annotated field date (which returns not null date from start or set date).
class TimerManager(models.Manager):
def get_queryset(self):
return (
super()
.get_queryset()
…

Vitalii Korniichuk
- 15
- 7
0
votes
1 answer
Access Foreign Key's Queryset
I have these models:
class Source(models.Model):
name = models.CharField(max_length=255, blank=True, null=True)
feed_url = models.CharField(max_length=512)
....
class Post(models.Model):
source =…

haduki
- 1,145
- 5
- 23
0
votes
1 answer
having problem to use annotate in django to return nested list
I have a model in django named Courses and it has a ManyToManyField which is related to the teachers Model. now in the views file I'm gonna make an APIView to return a list of courses which each courses has a field that returns teachers names in…
0
votes
0 answers
Django full text search using SearchHeadline for multiple fields
While using django full text search can we use SearchHeadline for mutiple field as of now if only takes one field to annotate headline.
search_vector = SearchVector('title', 'body')
search_query = SearchQuery(search_post)
search_headline =…

saibhaskar
- 435
- 6
- 21
0
votes
1 answer
Django ORM: This queryset contains a reference to an outer query and may only be used in a subquery
In the queryset, I want to get the average of what subquery returns and then group by 'store_id' and 'avg_sales'. However, when I used the following queries:
subquery = StoreStatistics.objects.filter(
store=OuterRef("store_id"),
…

Jasur
- 99
- 2
- 6
0
votes
0 answers
“Unexpected ORM Query Translation: CAST and Unnest Functions in Django”
I'm trying to execute a query using Django ORM to obtain the following SQL query:
python: 3.9.16
django: 3.2.19
value = text[]
SELECT CAST(x AS float)
FROM public.collection_basic_arrays
CROSS JOIN unnest(value) AS t(x)
WHERE x::float BETWEEN 0.001…

zN3utr4l
- 21
- 1
- 2
0
votes
1 answer
group by then annotate following relationships backwards in django
I can sum all payments for each Klient. But I need to know sum each type of payment.
Here are my models:
class Klient(models.Model):
name = models.CharField(max_length=30)
surname = models.CharField(max_length=30)
class…

janci
- 49
- 6
0
votes
0 answers
How to remove an annotate attribute from a Django query set?
I have a situation where I am annotating some values and I do not want those to be the output.
qs = A.objects.annotate(data=Cast('amount', FloatField()))
I want to remove data before the serialization in my Django projects
Note - This is…

Iceman
- 157
- 1
- 9
0
votes
0 answers
Defining the unit of measurement when annotating a django queryset using Distance
I am very new to django / python. I am currently experimenting building a web application using GeoDjango on MySQL/Spatialite (later I will move to postgres/postGIS when I decide to pay for a dedicated environment).
I use the following expression to…
0
votes
0 answers
Django annotate with dynamic value
Hi in my model i have property that fully is in cache, not in database
class Model(models.Model):
....
@property
def _get_last_seen_cache_name(self) -> str:
return f'{self.hostname.upper()}_last_seen'
@property
def…

Elendiar
- 37
- 5
0
votes
1 answer
Django - Annotating, Filtering and Sorting on a none to many relationships doesn't work
I am working on a table that display a list of orders with the option to filter and to sort depending either to direct attribute of the order or indirectly to attribute of related models. The two models in question are Order and Block. An Order can…

Quentin Merci
- 1
- 3