Questions tagged [django-aggregation]

django-aggregation refers to an ability to aggregate a collection of objects provided by Django database-abstraction API

django-aggregation refers to an ability to aggregate a collection of objects provided by Django database-abstraction API.

See documentation.

206 questions
12
votes
1 answer

Using an aggregate in a Django model's property

Schema Description A project could have multiple project items, and a project item could have multiple acquisition costs. For example, capital project #1 contains two project items: Qty. 1 production equipment purchased One acquisition cost of…
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
11
votes
2 answers

Get daily counts of objects from Django

I have a Django model with a created timestamp and I'd like to get the counts of objects created on each day. I was hoping to use the aggregation functionality in Django but I can't figure out how to solve my problem with it. Assuming that doesn't…
guidoism
  • 7,820
  • 8
  • 41
  • 59
10
votes
1 answer

Display Django values() on Foreign Key in template as object instead of its id

I have a queryset in Django that calls Model.objects.values('item')... where 'item' is a Foreign Key. class Words(models.Model): word = models.CharField() class Frequency(models.Model): word = models.ForeignKey(Words) ... So this returns…
simi
  • 103
  • 1
  • 1
  • 5
9
votes
1 answer

Grouping Django model entries by day using its datetime field

I'm working with an Article like model that has a DateTimeField(auto_now_add=True) to capture the publication date (pub_date). This looks something like the following: class Article(models.Model): text = models.TextField() pub_date =…
Michael Mulich
  • 1,240
  • 14
  • 21
9
votes
2 answers

Using Subquery to annotate a Count

Please help me I've been stuck on this for way too long :( What I want to do: I have these two models: class Specialization(models.Model): name = models.CharField("name", max_length=64) class Doctor(models.Model): name =…
7
votes
3 answers

Django: get aggregated value of two multiplied columns

I need to get aggregated value of two columns. So first multiple them together and then get theirs sum(). Code below naturally does not work, it is just for clarification. Is it somehow possible or should I use raw SQL? SomeModel.objects …
yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
6
votes
1 answer

How to query the maximum of counts of related records satisfying a predicate

I am working on a Django app for which I have encountered a tricky aggregate query that I would like to evaluate. In the demo app of my project, I declared the following model classes for representing libraries' holdings: from django.db import…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
6
votes
3 answers

django annotate models with an aggregate value based on query

Let's say I have the following model structure: Parent(): Child(): parent = ForeignKey(Parent) GrandChild(): child = ForeignKey(Child) state = BooleanField() num = FloatField() I'm trying to from the Parent ViewSet, recover the following: The…
6
votes
2 answers

Django ORM, sum of multiple columns

I have a question about how we can filter by SUM of multiple columns. Example: class Foo(models.Model): i1 = models.IntegerField() i2 = models.IntegerField() i3 = models.IntegerField() And I need to filter objects where SUM of i1, i2,…
wowbrowser search
  • 345
  • 1
  • 3
  • 11
5
votes
2 answers

How to annotate sum over Django JSONField (Array of objects) data?

I have models sth like this # models.py class MyModel( models.Model ): orders = models.JsonField(null= True, blank=True, default=list) category = models.ForeignKey(Category, on_delete=models.CASCADE) I stored json data in this structure. [ …
5
votes
1 answer

It is possible to GROUP BY multiple columns separately and aggregate each one of them by other column with django ORM?

I know how to GROUP BY and aggregate: >>> from expenses.models import Expense >>> from django.db.models import Sum >>> qs = Expense.objects.order_by().values("is_fixed").annotate(is_fixed_total=Sum("price")) >>> qs
Murilo Sitonio
  • 270
  • 7
  • 30
5
votes
1 answer

Complex count across many to many field in Django ORM

So I have a set of tasks that can appear in many categories: class TaskGroup(models.Model): name = models.CharField(max_length=200) slug = models.SlugField(max_length=200) icon = models.CharField(max_length=200, blank=True, null=True) …
pip
  • 453
  • 2
  • 13
5
votes
1 answer

Django ORM: Filter on timedelta of Datetime fields

I am trying to fetch posts based on the time difference of two DateTimeFields, say, posts that were deleted in less than 10 minutes after they were posted. class Post(models.Model): ... time_posted = models.DateTimeField() time_deleted =…
onurmatik
  • 5,105
  • 7
  • 42
  • 67
5
votes
1 answer

Django aggregation over choices

I have a following model: class VotingRound(models.Model): pass # here are some unimportant fields class Vote(models.Model): voting_round = models.ForeignKey(VotingRound) vote = models.CharField(choices=...) Now I have instance of…
Visgean Skeloru
  • 2,237
  • 1
  • 24
  • 33
5
votes
1 answer

Django Include Aggregate Sums of Zero

I'm working on a Django timesheet application and am having trouble figuring out how to include aggregate sums that equal zero. If I do something like: entries = TimeEntry.objects.all().values("user__username").annotate(Sum("hours")) I get all…
tomas
  • 51
  • 1
1
2
3
13 14