16368

how to add property/attribute/??? to queryset (like annotation)

Question:

I have two querysets:

category_list_avg =Category.objects.filter(operation__date__gte = year_ago).annotate(podsuma = Sum('operation__value')) category_list = Category.objects.annotate(suma = Sum('operation__value'))

Is there a way to do this with one queryset (there is a for loop in my template)?

<strong>UPD</strong>:

template:

{% for category in category_list %} <tr> <td><a href="{{ category.id }}/">{{ category.name }}</a></td> <td style="text-align:right{% if category.suma|default:0 < 0 %};color:red{% endif %}"> {{ category.suma|default:0|currency }}</td> {% for monthsum in category.get_month_sum_series %} <td style="text-align:right{% if monthsum.1 < 0 %};color:red{% endif %}">{{ monthsum.1|currency }}</td> {% endfor %} <td style="text-align:right">???</td> </tr> {% endfor %}

in place of ??? would be the average value (now I use sum of subpart - because its easier to debug - if I see that podsuma is less than suma - I would know that it works.

Answer1:

You need to do another query to get average values and attach them to category_list, either in view or in model:

# in view category_list = Category.objects.annotate(suma = Sum('operation__value')) one_year_ago_avg = dict(Category.objects.filter(operation__date__gte = year_ago).annotate(avg = Avg('operation__value')).values_list('pk', 'avg')) for cat in category_list: cat.one_year_ago_avg = one_year_ago_avg.get(cat.pk) # or in model class Category(models.Model): def one_year_ago_avg(self): year_ago = ... return self.operation_set.filter(date__gte=year_ago).aggregate(Avg('value')).values()[0]

Then use {{ category.one_year_ago_avg }} in template

Recommend

  • droppable in droppable
  • argumentnullexception bitmap save to memorystream
  • Symfony YAML format conversion
  • WPF ClickOnce missed references
  • Scrolling News Ticker Jquery - Issues
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • Why does the font in these TD elements render at different sizes?
  • garbled css name when styling within UiBinder
  • How to open html table in xls on click of a button
  • how to get username into sql trigger when multiple users signed on from asp membership
  • Simulate click Geckofx vb,net
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Custom validator control occupying space even though display set to dynamic
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Jquery UI tool tip close icon
  • Read a local file using javascript
  • Highlight and Bold text in JTextPane
  • Xamarin Forms - UWP Fonts
  • How reduce the height of an mschart by breaking up the y-axis
  • HTML download movie download link
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • jquery mobile loadPage not working
  • Unanticipated behavior
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Android Google Maps API OnLocationChanged only called once
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal