My model has a def on it which returns a calculated field. In my template I’m displaying the fields of my model and my def calculated field displays fine too.
But when I use distinct() in the queryset then the def calculated field no longer appears in the template. Why?
Another question is that the foreign keys are now being displayed as their IDs instead of their unicode.
How can I get the calculated field to display and not have the ids but the usual unicode pull through. Is this possible using distinct()?
models.py
@property
def calculated_total(self):
aggregated_cost = sum([m.total for m in Fee.objects.filter(contract=self.contract,grouping=self.grouping,\
party_incurring_fee=self.party_incurring_fee,\
party_paying_fee=self.party_paying_fee)])
return aggregated_cost
views.py
calculated_subtotal_queryset = Fee.objects.values('party_incurring_fee', 'party_paying_fee', 'grouping').distinct()
context_dict = {
'Subtotal' : calculated_subtotal_queryset,
}
return render_to_response('contract.html', context_dict)
contract.html
{% for s in Subtotal %}
<tr>
<td>{{ s.calculated_total }}</td>
In the view you are passing a
ValuesQuerySetto the template, therefore the loop in your template gets dictionaries instead of a regular queryset containing model-instances. I don’t understand your second question but most probably it has to do with theValuesQuerySetagain.