annotate then filter then annotate


The code:

now = datetime.now() year_ago = now - timedelta(days=365) category_list = Category.objects.annotate(suma = Sum('operation__value')) \ .filter(operation__date__gte = year_ago) \ .annotate(podsuma = Sum('operation__value'))

The idea: get sum of each category and sum of one year back.

But this code result only filtered objects; suma is equal to podsuma.


A queryset only produces one query, so all annotations are calculated over the same filtered data set. You'll need to do two queries.


Do something like this:

In models.py

class Category(models.Model): ... def suma(self): return ... def podsuma(self): return ...

Then remove the annotations and your for loop should work as is. It'll mean a lot more queries, but they'll be simpler, and you can always cache them.


  • Android device not shown with adb
  • Get todays date in Javascript
  • Format MySQL using CONCAT
  • Parsing Java String to date
  • F# OleDb Syntax Error in INSERT INTO Statement Pulling Data from Access to Linked SQL Server
  • Pass by Value and Pass by Reference
  • Appending into another table and avoiding duplicates
  • A simple leap year logic trouble
  • Populating a DropDownList with text and values
  • how to calculate month difference in R
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • Android App with ASP.NET WebAPi Server - send complex types
  • How can I include javascript in content loaded with jquery using $.ajax
  • Is it possible to collect a stream to two different collections using one line?
  • Filtering SPARQL results by day and month
  • d3js: time scaling and “1901”
  • Finding for convolution kernel if many 0's for FFT?
  • CakePHP 3: change order in dateWidget
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Return to second to last URL in MVC (return View with previous filter conditions applied)?
  • How to get month name with year and list of years between two Date
  • Replace last two characters in column
  • Python pandas melting data to multiple columns and coulmn names in another column
  • Invalid Date on validation Date of js
  • MySQL Order by column = x, column asc?
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • TFS: Get latest causes slow project reloading
  • Java applet as stand-alone Windows application?
  • Updated Ionic CLI but shows previous version (Windows)
  • php design question - will a Helper help here?
  • retrieve vertices with no linked edge in arangodb
  • AngularJs get employee from factory
  • Error creating VM instance in Google Compute Engine
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • Python/Django TangoWithDjango Models and Databases