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

Django - aggregation with get_FOO_display

Consider the following: status = queryset.values('status').annotate(count=Count('status')) where status field is a CharField with choices. This will result in a list of dictionaries with status DB value along with its count. Is there a way to…
Aziz Alfoudari
  • 5,193
  • 7
  • 37
  • 53
4
votes
1 answer

Django - Query count of each distinct status

I have a model Model that has Model.status field. The status field can be of value draft, active or cancelled. Is it possible to get a count of all objects based on their status? I would prefer to do that in one query instead of…
Milano
  • 18,048
  • 37
  • 153
  • 353
4
votes
1 answer

Django: Annotate list of related fields

I have an Company and User models with a related model CompanyRecruiter: class CompanyRecruiter(models.Model): organization = models.ForeignKey(Company, related_name="company_recruiters") recruiter = models.ForeignKey(User,…
4
votes
2 answers

Django: Problem in doing complex annotation and aggregation

This is model: class Purchase(models.Model): date = models.DateField(default=datetime.date.today,blank=False, null=True) total_purchase = models.DecimalField(max_digits=10,decimal_places=2,blank=True, null=True) I want to perform…
Niladry Kar
  • 1,163
  • 4
  • 20
  • 50
4
votes
0 answers

Django group by consecutive days (Friday, Saturday, Sunday)

I have an event that occurs everyday. Here is the model class Event(models.Model): date = models.DateField() plays = models.IntegerField() viewers = models.IntegerField() time = models.FloatField() I am trying to aggregate the…
4
votes
2 answers

Django aggregation query on related one-to-many objects

Here is my simplified model: class Item(models.Model): pass class TrackingPoint(models.Model): item = models.ForeignKey(Item) created = models.DateField() data = models.IntegerField() class Meta: unique_together =…
parxier
  • 3,811
  • 5
  • 42
  • 54
4
votes
2 answers

How to just retrieve the integer of a aggregate query in Django?

This might be a really dumb question, but how do I just retrieve the Integer using the query below? staravg = UserReview.objects.filter(name__username__iexact=username).aggregate(Avg('stars')) If I do {{ staravg }} in my template, I get…
stephan
  • 2,293
  • 4
  • 31
  • 55
3
votes
1 answer

Can I get a Decimal back from Avg on a DecimalField in Django?

When using the Avg aggregate for a models.DecimalField, the returned value is a float, instead of a decimal.Decimal. Is this because my specific database (in this case sqlite3) doesn't support aggregate values as decimals? If so, are there databases…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
3
votes
2 answers

Django- Group by and Count by unique together

I have following models: class Post(models.Model): title = models.CharField(max_length=30) class PostView(models.Model): post = models.ForeignKey(Post, related_name='views', on_delete=models.CASCADE) user =…
msln
  • 1,318
  • 2
  • 19
  • 38
3
votes
1 answer

Getting the ID of the Max record in an aggregate

Take these models: class Rocket(Model): ... class Flight(Model): rocket = ForeignKey(Rocket) start_time = DateTimeField(...) If I want to get start times of the latest flight for every rocket, that is simple: >>>…
frnhr
  • 12,354
  • 9
  • 63
  • 90
3
votes
0 answers

Django annotate with standard deviation

I´m trying to annotate a queryset by the Standard deviation. I found this post How to perform STD And read this documentation about querysets I tried this: from django.db.models import StdDev ventas_producto =…
Francisco Ghelfi
  • 872
  • 1
  • 11
  • 34
3
votes
2 answers

Django admin custom field fetching result of latest related object by condition

I have three models class Customer(models.Model): created = models.DateTimeField(auto_now_add=True) # some other fields, which don't matter class ActivityStatus(models.Model): created = models.DateTimeField(auto_now_add=True) name =…
3
votes
2 answers

How to filter Django annotations on reverse foreign key fields

I am trying to get a count of all related models with a particular field value. Here is some code... models.py: class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): BAD = "BAD" MEH = "MEH" GOOD =…
trubliphone
  • 4,132
  • 3
  • 42
  • 66
3
votes
2 answers

Annotating Django querysets with ForeignKey Counts subject to conditions

Here is a simplified version of my models: class Airport(models.Model): iata = models.CharField() name = models.CharField() latitude = models.FloatField() longitude = models.FloatField() class Flight(models.Model): origin =…
3
votes
1 answer

Django annotate count of foreign key's foreign key

I have three main models: Class Client(models.Model): stuff Class Property(models.Model): client = models.ForeignKey(Client) branch = models.ForeignKey(Branch, blank=True, null=True) Class Branch(models.Model): name =…
Andrew
  • 1,000
  • 1
  • 17
  • 33
1 2
3
13 14