I have a sorted QuerySet I am passing to a template via dict. I need to maintain the QuerySet sort order through the dict to dynamically populate a table.
I am attempting to solve this problem via a nested list as suggested by This Answer. I have appended this solution with This Suggestion.
After Apache2 restart - the first load of the table has the correct sort. Once i click refresh, the sort is disrupted. I can click refresh multiple times and eventually get the sort back, but... Another Apache restart resets the sort order to expected.
Can anybody help me? Thx.
UPDATE:
Please, note finished_case
is a foreign key. finished_case
also inherits a Meta
class attribute ordering = ['name']
. But i also explicitly .order_by('finished_case__name')
.
views.url:
def caseindex(request):
qtylist = caseStockOnHand(request)
srtd = sorted(qtylist.items(), key=lambda item: item[0])
context = {'qtyonhand':srtd}
return render(request,'box_inv/caseindex.html', context)
methods.url:
def caseStockOnHand(request):
s = stocktake.objects.filter(stocktake_type__name='Case').latest('date')
c = caselog.objects.filter(date__gte=s.date)
sd = stocktake_detail_case.objects.filter(stocktake=s).order_by('finished_case__name')
qtylist = {}
#Get Stocktake Qty
for x in sd:
qtylist[x.finished_case] = x.qty
return qtylist
template.html:
{% for x, y in qtyonhand %}
{% if y > 0 %}
<tr class = {% cycle 'row1' 'row2' %}>
<td>{{x}}</td>
<td>{{y}}</td>
</tr>
{% endif %}
{% endfor %}
Also, I am using django 1.5, python 2.7, mysql 5.5, and Apache 2.2 with mod_wsgi on Debian Wheezy 7.5.