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

Django: SUM of COUNT in annotations

I have the following simple models: class Member(models.Model): name = models.CharField(max_length=100) class Booking(models.Model): date = models.DateField(default=now) price = models.DecimalField(max_digits=7, decimal_places=2) …
1
vote
1 answer

django orm - annotate / aggregation (avg) in subquery

I have this model: class UserMovieRel(models.Model): user = models.ForeignKey("register.User", on_delete=models.CASCADE) movie = models.ForeignKey("Movie", on_delete=models.CASCADE, related_name="users") rating =…
1
vote
0 answers

Django query to get frequency-per-day of events with a DateTimeField

I have a model with events that each have a DateTimeField and can readily write a query that gets me the count of events per day: MyEvents.objects.annotate(day=TruncDate('date_time')).values('day').annotate(count=Count('id')) which produces…
Bernd Wechner
  • 1,854
  • 1
  • 15
  • 32
1
vote
1 answer

In Django, how can I retrieve all the most recent versions of my objects?

Lets say I am modeling a VCS with Django, using a model something like this: class VCSItem(models.Model): directory = models.CharField() name = models.CharField() revision = models.IntegerField() Now (directory, name) uniquely identify…
Willi Ballenthin
  • 6,444
  • 6
  • 38
  • 52
1
vote
1 answer

Django aggregate field, but filtered by date

I,m trying to annotate a sum of another model, but filtered by date. I have the models Employee and Shift, the Shift one has a DecimalField called dur, a DateTimeField start and a foreign key employee. class Employee(models.Model): name =…
Jemx
  • 45
  • 5
1
vote
1 answer

Concatenate foreign key fields with delimiter Django

Lets say I have 2 models with a foreign key relation which are used to bundle books: class Bundle(models.Model): name = models.CharField(max_length=100) class Book(models.Model): name = models.CharField(max_length=20) isbn =…
S.D.
  • 2,486
  • 1
  • 16
  • 23
1
vote
1 answer

Django aggregate sum throwing `int() argument must be a string, a bytes-like object or a number, not 'dict'`

I am trying to create a serializer to aggregate some data regarding a users inventory, however it is throwing the following error: Exception Value: int() argument must be a string, a bytes-like object or a number, not 'dict' I am not sure what…
Ross
  • 2,463
  • 5
  • 35
  • 91
1
vote
1 answer

How to perform conditional aggregation on a Django model's one to many related field?

Given the following model schemas: class Transaction(models.Model): wallet = models.ForeignKey(related_name="transactions") amount = models.DecimalField() # must be positive value type = models.CharField(choices=[("deposit", "deposit"),…
bahmsto
  • 43
  • 5
1
vote
3 answers

Django aggregate sum for each user

I'm creating a budget application and wanted to find the sum of each user who's signed in. Right now, I'm using function-based views and I sum up the expenses with Post.objects.aggregate(sum = Sum('expense')) The issue with this is it sums up…
1
vote
2 answers

Django order_by() is not working along with distinct()

How can i use order_by() in combination with distinct() ? I am having multiple same run_id with different end_time and trying to filter distinct run_id and order by end_time data =…
1
vote
1 answer

Django- How to get element index in a queryset by using F expression

I have following models: class Topic(models.Model): title = models.CharField(max_lenght=32) # Some other fields. class Thread(models.Model): topic = models.ForeignKey(Topic, related_name=threads', on_delete=models.CASCADE) # some…
msln
  • 1,318
  • 2
  • 19
  • 38
1
vote
1 answer

Django Annotate and Aggregate

I'd like to Sum the post_value of all of the Posts for each post_user to eventually use in a chart. I'm struggling with how to formulate the query? So far, I've got to: user_totals =…
1
vote
1 answer

Django Count with filter

iam trying to count with filter and it doesn't show any error but there is no count showing I have tried all what I know and search every where didn't find any solution : class UsersAnswersSerializer(serializers.ModelSerializer): Answers =…
Bosoud
  • 158
  • 4
  • 24
1
vote
0 answers

How to execute a django query with group by, alongwith multiplying two columns?

I have data like this product type quantity price/quantity chocolate buy 2 100 chocolate buy 4 200 colddrink buy 3 300 chocolate sell 3 200 colddrink buy 1 100 now I want to group the data by product and its type, and the…
1
vote
2 answers

Count the leave days and display them in django admin model

I am learning django or a month now. I created a model called "Leave" to mark employees leave. Then I created a model called "Salarie".In this I need to create a field like "Total_Leave" which will show an employees leave count in a month.( In…
Deepika
  • 737
  • 7
  • 23