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

Is it "better" to have an update field or COUNT query?

In a Django App I'm working on I've got this going on: class Parent(models.Model): name = models.CharField(...) def num_children(self): return Children.objects.filter(parent=self).count() def avg_child_rating(self): …
0
votes
1 answer

django: SQL UNION or concatenate with queryset: not working properly when used with annotate

I have the below two querysets books1 and books2. On both i am using annotate to add another column with a constant of 10 for books1 and 30 for books2. The problem is after i Concatenate the querysets the final set shows the annotate value = 10 even…
Santhosh
  • 9,965
  • 20
  • 103
  • 243
0
votes
1 answer

Invalid distinct with django

I try to get a cars list on my Django project but i'm in trouble with ORM class Car(models.Model): name = models.CharField(max_length=200) owner = models.ForeignKey(User) With Car.objects.all() I have a list as: - car#1, user#1 - car#1,…
0
votes
1 answer

Conditional filtering on Count - Django

I want to count a number of all incorrect choices in question. I have a query for a number of all choices: questions.annotate(choices_count=Count('choices')) Now, when I want to filter incorrect choices only into the Count function, it…
Milano
  • 18,048
  • 37
  • 153
  • 353
0
votes
1 answer

Django: Count by day, with 0 for days with no records

I want to get the count of records over 14 days I'm currently doing: class Invitation(models.Model): ... start_date = models.DateTimeField(default=timezone.now) all_invitations = Invitation.objects.all() days14 =…
blue_zinc
  • 2,410
  • 4
  • 30
  • 38
0
votes
1 answer

Django annotate : count of ModelB entries not in manytomany relationship with ModelA

In this particular case ModelA is the default Django models.User ModelB is implemented as : class ModelB(models.Model): user = models.ManyToManyField(User) #some other stuff 1. For each ModelB record, count the User records which don't…
0
votes
1 answer

Django group FK by field

I'm working on a simple keyword grouping tool. Phrase represents keyword in search engine. SerpEntry represents link on site. I'm looking for a way to group Phrases by SerpEntry urls. Here are my models: class Phrase(models.Model): text =…
Alex T
  • 4,331
  • 3
  • 29
  • 47
0
votes
2 answers

How to aggregate data in django?

I have model whose instance creating every hour: class Entry(models.Model): class Meta: ordering = ['time'] time = models.DateTimeField() no2 = models.FloatField() co = models.FloatField() humidity = models.FloatField() …
Vassily
  • 5,263
  • 4
  • 33
  • 63
0
votes
1 answer

Annotating Django querysets with counts using multiple fields

Here is a simplified version of my model: class Flight(models.Model): airline = models.CharField(max_length=100) origin = models.CharField(max_length=4) destination = models.CharField(max_length=4) What I would like to do is group the…
jc315
  • 107
  • 1
  • 9
0
votes
1 answer

How to count all ForeignKey models with exact field values in Django?

I'm not sure if it's possible or not, but I want to count all votes associated with a model with an exact vote_type attribute. Here's the model: class Link(models.Model): title = models.CharField(max_length=200) . . . class…
0
votes
1 answer

Wrong GROUP BY field in Django annotate query

The original problem caused by quite awkward cycling models reference: # A -> B -> A class A: b = models.ForeignKey('B', null=True, blank=True) class B: a = models.ForeignKey('A') Now, when I'm trying to annotate query, it always uses…
ZAN
  • 561
  • 1
  • 4
  • 16
0
votes
1 answer

Django aggregation escapes unicode strings?

I tried to use aggregation on a database with unicode texts and it showed unicode object with unicode characters encoded another time. What can I do to show the unicode text after aggregation? >>> from apps.person.models import Person >>> from…
int_ua
  • 1,646
  • 2
  • 18
  • 32
0
votes
2 answers

Django aggregation in templates?

I'm thinking a bit about the concept of Django aggregates. I don't quite "get" how they can be used in my case. Basically i have a three-tier hierarchy of objects in my model, and the lowest object (Bar) contain values I want to aggregate. class…
pojo
  • 5,892
  • 9
  • 35
  • 47
0
votes
1 answer

Django related _set with order_by showing duplicates

With Django 1.7, using following code in my view: driver = get_object_or_404(Driver, id=self.object.id) cars = driver.car_set.order_by('model__market_date') for car in cars: # for testing only print car.id # outputs e.g. 3, 3, 3, 5 When I try…
SaeX
  • 17,240
  • 16
  • 77
  • 97
0
votes
1 answer

Django select count of users acquired points in range

I have table like that: user_id | points --------+-------- 1 10 1 500 2 2 2 6 3 5 3 50 Now I want to calculate how many users has SUM(points) between, for example, between 1 and 80, betwee 81 and 140,…
Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
1 2 3
13
14