Hello,
I want to get the Volume subtotal of my “Contracts” field grouped in my template.
I manage to display the total but not the subtotal.
Here are my files:
views.py
class TransportDetListView(LoginRequiredMixin,ListView):
login_url = 'admin:login'
model = TransColisCgDlaDet
template_name = "betou/transports/sciages/trans_detail.html"
paginate_by = 15
def get_queryset(self):
queryset = TransColisCgDlaDet.objects.filter(code_trans__contains=self.kwargs['code_trans']).order_by('num_contrat','essence','epaisseur')
lstcolis = queryset.values(
'num_colis',
'essence',
'epaisseur',
'qualite',
'produit',
'num_contrat',
'code_specif',
'code_specif_douane',
'destinataire',
'port_destination',
'marque',
'receptionnaire',
'code_trans'
).annotate(volumecolis=Sum('cubage')).annotate(nb_elts=Sum('nbre_elts'))
return lstcolis
def get_context_data(self, **kwargs):
context = super(TransportDetListView, self).get_context_data(**kwargs)
context['codetrans_filter'] = TransColisCgDlaDet.objects.filter(code_trans__contains=self.kwargs['code_trans']).order_by('num_contrat','essence','epaisseur')
context['nbrecolis'] = context['codetrans_filter'].values('num_colis').order_by('num_contrat').aggregate(nbrecolis=Count('num_colis')).get('nbrecolis')
context['totalvolume'] = context['codetrans_filter'].values('cubage').aggregate(totalvolume=Sum('cubage')).get('totalvolume')
context['totalelts'] = context['codetrans_filter'].values('nbre_elts').aggregate(totalelts=Sum('nbre_elts')).get('totalelts')
context['contrats'] = context['codetrans_filter'].annotate(volumecolis=Sum('cubage')).annotate(nb_elts=Sum('nbre_elts'))
return context
template
{% regroup contrats by num_contrat as lst_group %}
<table class="table table-responsive table-bordered text-center">
{% for group_ct in lst_group %}
<thead>
<tr>
<td colspan = "15" class="align-center" style="font-weight: 600">{{ group_ct.grouper }}</td>
</tr>
<tr>
<tr>
<th>N.Contrat</th>
<th>Essence</th>
<th>Epais.</th>
<th>N.Colis</th>
<th>Nb Elts</th>
<th>Volume</th>
<th>Qualité</th>
<th>Produit</th>
<th>Specif</th>
<th>Specif Douane</th>
<th>Destinataire</th>
<th>Destination</th>
<th>Marque</th>
<th>Réceptionnaire</th>
<th>Code Trans</th>
</tr>
</thead>
<tbody>
{% for trans in group_ct.list %}
<tr>
<td>{{trans.num_contrat|default_if_none:""}}</td>
<td>{{trans.essence}}</td>
<td>{{trans.epaisseur}}</td>
<td>{{trans.num_colis}}</td>
<td style="text-align: right;">{{trans.nb_elts}}</td>
<td style="text-align: right;">{{trans.volumecolis}}</td>
<td>{{trans.qualite|default_if_none:""}}</td>
<td>{{trans.produit|default_if_none:""}}</td>
<td >{{trans.num_contrat}} {{trans.code_specif}}</td>
<td>{{trans.code_specif_douane|default_if_none:""}}</td>
<td>{{trans.destinataire|default_if_none:""}}</td>
<td>{{trans.port_destination|default_if_none:""}}</td>
<td>{{trans.marque|default_if_none:""}}</td>
<td>{{trans.receptionnaire|default_if_none:""}}</td>
<td><a href="{% url 'betou:transport_codetrans' trans.code_trans %}" class="link text-secondary">{{trans.code_trans}}</a> {% if not forloop.last %}{% endif %}</td>
</tr>
{% endfor %}
<tr>
<td colspan = "3" style="text-align: center; font-weight: 600;">TOTAUX:</td>
<td style="text-align: right; font-weight: 600;">{{ group_ct.list|length }} Colis</td>
<td style="text-align: right; font-weight: 600;">{{ group_ct.list.nb_elts }}</td>
<td style="text-align: right; font-weight: 600;"> {{ group_ct.list.volumecolis }}</td>
<td colspan = "12"></td>
</tr>
{% endfor %}
<tr>
<td colspan = "3" style="text-align: center; font-weight: 600;">TOTAUX:</td>
<td style="text-align: right; font-weight: 600;">{{ nbrecolis }} Colis</td>
<td style="text-align: right; font-weight: 600;">{{ totalelts }}</td>
<td style="text-align: right; font-weight: 600;"> {{ totalvolume }}</td>
<td colspan = "12"></td>
</tr>
</tbody>
</table>
I get this result:
You see that the Parcel count is done correctly {{ group_ct.list|length }}. But for the sum I don’t have a subtotal result.
Thanks