Questions tagged [django-annotate]
297 questions
2
votes
2 answers
How to group by and get latest record in group contain all of fields in Django?
Imagine we have a table like this:
id
name
type
created_at
1
James
male
2022-03-02
2
Jane
female
2022-04-02
3
Kirk
male
2022-03-04
4
Sarah
female
2022-04-04
5
Jason
male
2022-03-05
And i want to group by type and just get latest…

reza_khalafi
- 6,230
- 7
- 56
- 82
2
votes
1 answer
sorting in django admin list by a custom field alphabetically
I have a simple store / product relationship and I want to sort the products depending on the store name alphabetically.
models:
class Store(models.Model):
name = models.CharField("name", max_length = 128)
show_name =…

xtlc
- 1,070
- 1
- 15
- 41
2
votes
1 answer
Count elements in Django
I am learning Django. I have a question about counting elements from tables.
class Opinion(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
users = models.ForeignKey(User,…

gregory_000
- 35
- 3
2
votes
3 answers
query to django model to compare daily sale over previous day (compare two row of database model)
I have a django model that is "DailyReport" of the companies sale I want to find out company sale change over the previous day.
the model that i define is like that:
class DailyReport(models.Model):
company = models.CharField(max_length=50)
…

kazem qanati
- 117
- 7
2
votes
2 answers
query to django model to find best company sale in the month
I have two django model one "company" and the other is "MonthlyReport" of the company
I want to find out which company sale in current month had more than 20% of previous month sale
class Company(models.Model):
name =…

kazem qanati
- 117
- 7
2
votes
1 answer
Month on month values in django query
I have an annotation like this: which displays the month wise count of a field
bar = Foo.objects.annotate(
item_count=Count('item')
).order_by('-item_month', '-item_year')
and this produces output like this:
html render
I would like to show the…

Oneflydown
- 33
- 4
2
votes
1 answer
Django annotating fields with null values
I have list of Demand objects that have allocated field that would either be null or have a name (denoting this demand's allocation).
I use annotations to count allocated/unallocated numbers per…

abolotnov
- 4,282
- 9
- 56
- 88
2
votes
2 answers
How to get Substr when annotate Django
I'm try to sort by order_number had many kind
Input:
ADC123
ADC14
ADC23
ERD324
ERD12
Sort default just sort by alphabet
Expected results (Sort only by number):
ERD12
ADC14
ADC23
ADC123
ERD324
Code example:
Person.objects.annotate(
…

Queen
- 21
- 2
2
votes
0 answers
Is Nested aggregate queries possible with Django queryset
I want to calculate the monthly based profit with the following models using django queryset methods. The tricky point is that I have a freightselloverride field in the order table. It overrides the sum of freightsell in the orderItem table. An…

Ozzy Black
- 53
- 6
2
votes
1 answer
Group by date in Django
I am trying to achieve the result of the following SQL query
SELECT
UNIX_TIMESTAMP(DATE((FROM_UNIXTIME(`timestamp`)))) AS `x`,
COUNT(`timestamp`) as y
FROM somedb.events
WHERE user_id=3 AND
`timestamp` > 1612117800 AND
…

haccks
- 104,019
- 25
- 176
- 264
2
votes
2 answers
Django: How to "join" two querysets using Prefetch Object?
Context
I am quite new to Django and I am trying to write a complex query that I think would be easily writable in raw SQL, but for which I am struggling using the ORM.
Models
I have several models named SignalValue, SignalCategory,…

MetaHG
- 21
- 3
2
votes
1 answer
Annotated queryset, same name as property on model instance not working
I would like some property to always be there on a model instance. But I also need to annotate it on a queryset for some views. Is this possible?
Pseudo-code:
Friend(models.Model):
name= models.CharField()
@property
def…

gabn88
- 781
- 8
- 23
2
votes
0 answers
Django annotated queryset - resulting SQL query duplicates
Let's imagine I have two models: User and Event:
class User(models.Model):
email = models.EmailField(_('Email'), max_length=255, unique=True, db_index=True)
class Event(models.Model):
class Type(models.IntegerChoices):
ONE = 1,…

Hal
- 537
- 4
- 13
2
votes
1 answer
Complex Django ORM Annotations & Aggregations
I'm currently preparing some logged items into a JSON serialized format.
I am attempting to do this via Django's built-in ORM, utilising annotations and aggregations.
I'm trying to replicate the following structure per each of these "logged…

Micheal J. Roberts
- 3,735
- 4
- 37
- 76
2
votes
1 answer
Django compute score of matches (workaround to annotate a query after a union)
I need to compute the ranking of athletes during boxing tournaments. In my models, I track the result of each match and the points to be attributed for each outcome to each athlete.
class Member(models.Model):
surname =…

cgaspoz
- 631
- 1
- 5
- 11