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
0
votes
1 answer

Aggregate annotated field from Subquery in Django

I'm trying to implement a subquery with Django ORM, but I can't find a working solution. The SQL query that I would need to reverse-engineer is: select t1.location, sum(t1.value_relative::numeric) as total from ( select …
0
votes
1 answer

Grouping aggregations in Django

For reporting I want to group some counts together but still have access to the row data. I feel like I do this a lot but can't find any of the code today. This is in a B2B video delivery platform. A simplified version of my…
Oli
  • 235,628
  • 64
  • 220
  • 299
0
votes
1 answer

Django Queryset creating a .values() based on the sorting of another field

I'm trying to the most likely predicted category for a datapoint. Since code is the best explanation: models: class DataPoint(models.Model): #... unimportant fields class PredResult(models.Model): likelihood = models.FloatField() value…
JudoWill
  • 4,741
  • 2
  • 36
  • 48
0
votes
1 answer

Aggregate number of likes for each day within period

I am building a REST Api on Django RF which represents a very basic social network. I have a model Post that has a value Likes which is related to a User by many to many through a Like model. My models: class Post(models.Model): content =…
0
votes
1 answer

Average age from birth date in django

In my django(3.0.5) application I am trying to get average age from date of birth in a model. I am using MySql database. Here is how I tried: Model: class ShippingStaff(models.Model): full_name = models.CharField('Full Name', max_length=200) …
0
votes
0 answers

Django and aggregating grand-child records

I am new to Django but have been around RdB for a while. I am finally getting the hang of model-view-template. I am struggling a little on "aggregate" and "annotate" especially when my model has grand-child records and I want aggregate. I use Django…
0
votes
1 answer

SUM columns in Django

I have a simple model that collects data (specifically integers) to display an election results in a table. I have 2 regions (more of course but let's say two for now) where data will be coming from. I want to display each regions and the total for…
0
votes
1 answer

Filter django queryset: Query objects with two or more reverse-related objects with a certain identical field value

Suppose we have two models like this: from django.db import models ModelA(models.Model): ... ModelB(models.Model): a = models.ForeignKey(ModelA, ..., related_name='b_models') some_field = models.CharField(...) other_field = ... …
0
votes
1 answer

Dynamic views in Django ORM - best approach?

Some background I am considering rebuilding an existing Laravel website with Django. It's a website that allows sharing benchmark data from drone/UAV propulsion components. Some benchmarks are done while testing multiple motors and propellers at the…
0
votes
1 answer

How to use .annotate() or .aggregate() to extract a count from Django QuerySet relations?

I have the follow .model structure: class ArtistWorkPlaceQuerySet(models.QuerySet): def with_related(self): return self.select_related('artist','work', 'place') class ArtistWorkPlaceManager(models.Manager): pass class…
0
votes
1 answer

django ORM problem - Annotate and aggregate return inconsistent values

I found a setup where aggregate() and annotate() do NOT return the same values... I am really confused. I have the following models: class Invoice(models.Model): pass class InvoiceItem(models.Model): invoice = models.ForeignKey(Invoice,…
Ron
  • 22,128
  • 31
  • 108
  • 206
0
votes
0 answers

Can I annotate on a OneToMany relationship for performance?

I have the following simplified models. class Service(models.Model): ... class ServiceLogs(models.Model): service = models.ForeignKey(Service, related_name='logs', ...) who_did = models.ForeignKey(User, ...) role_played =…
0
votes
1 answer

How to update the total score in a trivia game backend efficiently?

I am trying to implement a trivia game backend in Django. I have several models but I will mention two of them which are Score and Player. Here is the important part of Player: class Player(BaseModel, AbstractUser): overall_score =…
Ambitions
  • 2,369
  • 3
  • 13
  • 24
0
votes
1 answer

How do I annotate a django queryset with StringAgg or ArrayAgg concatenating one column from mulitple children rows?

Documents is the parent table. Paragraphs is the child table. Users filter Documents based on various search criteria. Then I wish to annotate Documents with certain Paragraphs filtered by a text query. The same text query is used to filter…
edyas
  • 1
  • 1
  • 2
0
votes
1 answer

Django Aggregation: How I can get Max value only?

First of all, I'm using Django 2.2 and Python 3.7 I have this in my models.py class Test(models.Model): session = models.IntegerField() x_pos = models.IntegerField() y_pos = models.IntegerField() What am trying to do is to get the max…