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
1
vote
1 answer

converting sql to Django ORM

How to convert this SQL to Django ORM SELECT `table1`.*, `tabl2`.* FROM `table1` INNER JOIN `table2` ON (`table1`.`table2_id` = `table2`.`id`) INNER JOIN (SELECT table2_id, MAX(date) AS max_date FROM table1 WHERE date <=…
1
vote
0 answers

Django query aggration with on itself join

With the following model, I would like to select root users having the most posts in validated state (in a single query or at least in an optimized way). In my case root user are not directly authors, I want to aggregate children user authors on…
srjjio
  • 930
  • 1
  • 8
  • 16
1
vote
1 answer

Simple Django Aggregation - Optimization

I'm reading the beginnings of annotating and aggregating and I'm wondering which way is best to complete the following situation: You want to count the number of authors for each book in a queryset. The tutorial suggests the following with…
qarthandso
  • 2,100
  • 2
  • 24
  • 40
1
vote
1 answer

Django model group by ManyToMany matches count

I'm working on a simple keyword grouping tool. I want to group phrases by count of same urls in SERP. Phrase represents keyword in search engine. SerpEntry represents link on site. I'm looking for a way to select Phrases that have at least N same…
Alex T
  • 4,331
  • 3
  • 29
  • 47
1
vote
1 answer

Django model aggregate matches count in ManyToMany

There is Model with ManyToMany field: class Number(Model): current_number = IntegerField() class MyModel(models.Model): numbers_set = models.ManyToMany(Number) For example we have such dataset: my_model_1.numbers_set = [1, 2, 3,…
Alex T
  • 4,331
  • 3
  • 29
  • 47
1
vote
1 answer

Instantiating the SQL implementation of Aggregates in Django 1.8

I've been working on updating the existing code base that was using Django 1.6 to Django 1.8. In the process, I've been facing a particular problem with aggregates. In this code, PGDAggregate class has a method add_to_query which is intended to…
Sentient07
  • 1,270
  • 1
  • 16
  • 24
1
vote
1 answer

Using Django, how I can achieve this in a single query?

I have the following model: class Ticket(models.Model): # ... other fields omitted active_at = models.DateTimeField() duration = models.DurationField() Given now = datetime.now(), I'd like to retrieve all records for which now is…
Johnny 5
  • 1,521
  • 3
  • 10
  • 11
1
vote
1 answer

Django 1.6 + MySQL : Type Cast MySQL variable to search for Max, Avg

My Model is sort of like class ServiceUtilization(models.Model): device_name = models.CharField() service_name = models.CharField() data_source = models.CharField() current_value = models.CharField() sys_timestamp = models.IntegerField() Now, here…
Joddy
  • 2,557
  • 1
  • 19
  • 26
1
vote
1 answer

Django model manager didn't work with related object when I do aggregated query

I'm having trouble doing an aggregation query on a many-to-many related field. Here are my models: class SortedTagManager(models.Manager): use_for_related_fields = True def get_query_set(self): orig_query_set =…
satoru
  • 31,822
  • 31
  • 91
  • 141
1
vote
1 answer

django count annotation field

Django 1.3, Python 2.7 I have the following models (some irrelevant fields omitted): class Encounter(models.Model): subject = models.ForeignKey('Subject', to_field='uuid') uuid = models.SlugField(max_length=36, unique=True, default=make_uuid,…
user2592232
  • 145
  • 1
  • 1
  • 8
1
vote
1 answer

django query aggregation grouping with access to all fields

With models defined like so: class Athlete(models.Model): name = models.CharField() class Event(models.Model): winner = models.ForeignKey(Athlete) distance = models.FloatField() type_choices = [('LJ', 'Long Jump'), ('HJ', 'High…
Ferguzz
  • 5,777
  • 7
  • 34
  • 41
1
vote
1 answer

How to obtain a count of objects per auth.user?

I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I want to use django aggregation to return all users with a count of projects per user. Something…
cethegeek
  • 6,286
  • 35
  • 42
1
vote
1 answer

How to create annotation on filtered data in Django ORM?

I have the following models: class ApiUser(models.Model): apikey = models.CharField(max_length=32, unique=True) class ExtMethodCall(models.Model): apiuser = models.ForeignKey(ApiUser) method = models.CharField(max_length=100) #method…
Dmitry Nedbaylo
  • 2,254
  • 1
  • 20
  • 20
1
vote
2 answers

Django Aggregation of values queryset

I have a ValuesQuerySet called data. I am trying to get a summary count of all the type for each object data.values('type') produces this output: [{'type': u'internal'}, {'type': u'internal'}, {'type': u'external'}, {'type': u'external'}] I want to…
Austin
  • 4,296
  • 6
  • 40
  • 52
1
vote
1 answer

How to calculate the duration using startTime & endTime fields in a QuerySet in Django?

I have this model: class TimeInterval(models.Model): startTime = models.DateTimeField() endTime = models.DateTimeField() How can I aggregate average time interval using only the QuerySet API? I tried this: qs = TimeInterval.objects.extra( …
Benji Mizrahi
  • 2,154
  • 2
  • 23
  • 38