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
3
votes
0 answers

Django nested aggregates with group_by

I have a query select avg(total) from (select date, sum(value) as total from table group by some_field, date) as res group by week(date) This is the way I'm getting sum of metrics for each date and show average result grouped by week.…
3
votes
0 answers

Use method as aggregate field

I have two models that go as follows (for simplicity's sake): class Bank(models.Model): name = models.CharField(max_length=250) class Person(models.Model): bank = models.ForeignKey(Bank) savings = models.IntegerField() debts =…
arielnmz
  • 8,354
  • 9
  • 38
  • 66
3
votes
3 answers

Calculate average exchange rate for time period

In Django I have model similar to this example: class Currency(models.Model): name = models.CharField(max_length=3, unique=True) full_name = models.CharField(max_length=20) class ExchangeRate(models.Model): currency =…
WBAR
  • 4,924
  • 7
  • 47
  • 81
3
votes
2 answers

Django : Count only non-empty CharField with annotate() & values()

Django recommends not using null on CharField, however annotate includes empty strings in the count. Is there a way to avoid that without excluding rows with empty string from the query? My question isn't simly how to achieve my query, but…
user
  • 17,781
  • 20
  • 98
  • 124
3
votes
1 answer

Django query - Is it possible to group elements by common field at database level?

I have a database model as shown below. Consider the data as 2 different books each having 3 ratings. class Book(models.Model): name = models.CharField(max_length=50) class Review(models.Model): book = models.ForeignKey(Book) …
user
  • 17,781
  • 20
  • 98
  • 124
3
votes
2 answers

sort django queryset by latest instance of a subset of related model

I have an Order model and order_event model. Each order_event has a foreignkey to order. so from an order instance i can get: myorder.order_event_set. I want to get a list of all orders but i want them to be sorted by the date of the last event. A…
3
votes
2 answers

Where are django's annotated fields stored on the model instance?

I am doing this: p = MyModel.objects.filter(user__username="me").annotate(friend_count=Count(friends)) when I look at: p[0]._meta.get_all_field_names() It returns everything defined on the model but not the annotated field 'friend_count' Is there…
9-bits
  • 10,395
  • 21
  • 61
  • 83
2
votes
2 answers

How to filter objects by number of ForeignKey objects WITHOUT using raw SQL?

Is this finally possible in Django? Without this feature, using ORM is kinda strange.
SuitUp
  • 3,112
  • 5
  • 28
  • 41
2
votes
3 answers

Is it possible to order by an annotation with django TastyPie?

I'm trying to order by a count of a manyToMany field is there a way to do this with TastyPie? For example class Person(models.Model): friends = models.ManyToMany(User, ..) I want PersonResource to spit out json that is ordered by the number of…
9-bits
  • 10,395
  • 21
  • 61
  • 83
2
votes
1 answer

Sum together list of F expressions

Is there a way to specify (in an annotation or aggregation) that a sequence of F expressions should be summed together without manually typing out F("first_prop") + F("second_prop") + ...? I want something similar to how python's sum() function…
Uche Ozoemena
  • 816
  • 2
  • 10
  • 25
2
votes
1 answer

Average calculated on the difference on a Min and Max value in Django

I have two simple tables in Django which looks like: class Session(models.Model): id = models.AutoField(primary_key=True) class Track(models.Model): id = models.AutoField(primary_key=True) session = models.ForeignKey(Session) when =…
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
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…
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…
2
votes
1 answer

Django annotate subquery's aggregation

So I have three models class Advert(BaseModel): company = models.ForeignKey(Company, on_delete=CASCADE, related_name="adverts") class Company(BaseModel): name = models.CharField(max_length=50) class OrderRating(BaseModel): …
2
votes
0 answers

Django aggregation function in Hstore field

I am using djagno postgres function from https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/ I need to use aggregation function in hstore field, but getting error... Here is my models.py def get_default_dict(): return…
Somil
  • 1,921
  • 1
  • 21
  • 35