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

How to group by over related model count in Django ORM

Let's say I have the following models: class Conversation(Model): ... class Message(Model): conversation = models.ForeignKey(Conversation) The following code counts the number of messages for each conversation: Conversation.objects \ …
Petter
  • 37,121
  • 7
  • 47
  • 62
2
votes
1 answer

django queryset aggregation count counting wrong thing

This is a continuation question from: Django queryset get distinct column values with respect to other column My Problem: Using aggregate count in Django counts the wrong thing in the queryset, or as far as I can see something that is not even in…
Oha Noch
  • 374
  • 1
  • 7
  • 22
2
votes
2 answers

Django annotate() on two step relationship confusion

I have the following 3 models: Category: date_start date_end active: bool Player: name: str age: int category = models.ForeignKey(Category) PlayerContact: contact_result: int player = models.ForeignKey(Player) In this case I…
Onilol
  • 1,315
  • 16
  • 41
2
votes
2 answers

How to aggregate barchart data for volumes of registrations per day from Django auth?

Django auth User model has a date_joined DateTimeField. Could this be used to aggregate a list with volumes of registrations per day for days (or other time periods) in a daterange? E.g.: [(21.01, 5), (22.01, 7), (23.01, 9), ...] What is the fastest…
2
votes
2 answers

Django annotate Avg on model field

I have these models: class Agency(models.Model): pass class User(models.Model): agency = models.ForeignKey(Agency) class Feedback(models.Model): rating = models.DecimalField() user = models.ForeignKey(User) and I want to…
2
votes
2 answers

Django: Count objects in a particular month

I have this model: class Person(models.Model): city = models.CharField(max_length=20, blank=True) added_date = models.DateField(default=datetime.date.today) I want to create a template/view that has a table of months and the number of people…
2
votes
1 answer

Django aggregation: Group and sum over a date range

I am fetching some user stats daily and recording them in a model as follows (irrelevant parts are stripped off for simplicity): class User(models.Model): group = models.CharField( choices=(('A', 'Group A'), ('B', 'Group B'), …
onurmatik
  • 5,105
  • 7
  • 42
  • 67
2
votes
4 answers

Django aggregation on a date range

I have been lurking and learning in here for a while. Now i have a problem that somehow i cannot see an easy solution. In order to learn django i am bulding an app that basically keeps track of booked items. What I would like to do is to show how…
klaut
  • 210
  • 3
  • 10
2
votes
2 answers

ProgrammingError when aggregating over an annotated & grouped Django ORM query

I'm trying to construct a query to get the "average, maximum and minimum number of items purchased per user". The data source is this simple sales records table: class SalesRecord(models.Model): id =…
ento
  • 5,801
  • 6
  • 51
  • 69
2
votes
1 answer

Nested annotation in Django

I have three models class ModelA(models.Model): name = CharField(max_length=100) class ModelB(models.Model): modela = ForeignKey(ModelA) class ModelC(models.Model): modelb = ForeignKey(ModelB) How can I print all ModelA objects with…
Jamgreen
  • 10,329
  • 29
  • 113
  • 224
2
votes
0 answers

Perform sum of two annotated values in Django

I have done annotation on Student objects so that it has two new fields -> project_count and member_count as follows: top_students = Student.objects.annotate(project_count= Count('project'), member_count = Count('member_student')) I would now like…
Sudip Kafle
  • 4,286
  • 5
  • 36
  • 49
2
votes
1 answer

Can't do multiple annotations with Django

I'm working with django-eztables to do the server-side processing for a datatable, and I want to add some fields involving aggregation (as mentioned here) The following each work fine individually: def get_queryset(self): qs =…
StephenTG
  • 2,579
  • 6
  • 26
  • 36
2
votes
2 answers

Django Aggregate by QuerySet?

I'm writing a league system and I want to display player rankings in each season sorted by points each player accumulated during that season. So far I managed to do it with code similar to this: class Player(models.Model): name =…
minder
  • 2,059
  • 5
  • 24
  • 39
2
votes
1 answer

django: time range based aggregate query

I have the following models, Art and ArtScore: class Art(models.Model): title = models.CharField() class ArtScore(models.Model): art = models.ForeignKey(Art) date = models.DateField(auto_now_add = True) amount =…
Hoff
  • 38,776
  • 17
  • 74
  • 99
1
vote
1 answer

Grouping by months in Django and retrieving as datetime.date()

I was looking around to see how to group a django queryset by months. I've finally found this very helpful piece of code: truncate_date = connection.ops.date_trunc_sql('day','timestamp') qs = qs.extra({'date':truncate_date}) return…
Fernando Ferreira
  • 798
  • 1
  • 11
  • 26