I’m experience strange issue while resulting the dict of dict in django template.
data = [{'hrs': 9.0, 'ld': u'pname', 'dn': u'TS', 'ist': u'TS', 'act': u'O', 'date': datetime.date(2011, 9, 19), 'id': 1119556},
{'hrs': 9.0, 'ld': u'pname', 'dn': u'TS', 'ist': u'TS', 'act': u'Ti', 'date': datetime.date(2011, 9, 21), 'id': 1119558},
{'hrs': 2.5, 'ld': u'pname', 'dn': u'TS', 'ist': u'dmin', 'act': u'POC', 'date': datetime.date(2011, 9, 20), 'id': 1119577},
{'hrs': 0.5, 'ld': u'pname', 'dn': u'SMgr', 'ist': u'SMgr', 'act': u'PL', 'date': datetime.date(2011, 9, 20), 'id': 1119578},
{'hrs': 2.0, 'ld': u'pname', 'dn': u'SMgr', 'ist': u'TS', 'act': u'Mting', 'date': datetime.date(2011, 9, 20), 'id': 1119579},
{'hrs': 8.0, 'ld': u'sname', 'dn': u'holiday', 'ist': u'holiday', 'act': u'PL', 'date': datetime.date(2011, 9, 19), 'id': 1119455},
{'hrs': 8.0, 'ld': u'sname', 'dn': u'holiday', 'ist': u'holiday', 'act': u'PL', 'date': datetime.date(2011, 9, 21), 'id': 1119457},
{'hrs': 1.0, 'ld': u'sname', 'dn': u'TS', 'ist': u'TS', 'act': u'O', 'date': datetime.date(2011, 9, 20), 'id': 1119566},
{'hrs': 7.0, 'ld': u'sname', 'dn': u'PD', 'ist': u'PD', 'act': u'LOP', 'date': datetime.date(2011, 9, 20), 'id': 1119567}]
Using this list of dict wrote below logic
results = collections.defaultdict(dict)
for fetch in data:
user = fetch['ld']
get_id = fetch['id']
adata = '%s/%s/%s/%s' % (fetch['dn'], fetch['ist'], fetch['act'], fetch['hrs'])
row = results[user]
row['user'] = user
dt = str(fetch['date'])
row[dt] = adata
this give me below output
Results :
{u'sname': {'2011-09-21': u'holiday/holiday/PL/8.0', '2011-09-20': u'PD/PD/LOP/7.0', 'user': u'sname', '2011-09-19': u'holiday/holiday/PL/8.0'},
u'pname': {'2011-09-21': u'TS/TS/Ti/9.0', '2011-09-20': u'SMgr/TS/Mting/2.0', 'user': u'pname', '2011-09-19': u'TS/TS/O/9.0'}}
In General i need to get below output, but dict of dict print only date key value instead of of multiple key values on same date
Expected Output :
{u'sname': {'2011-09-21': u'holiday/holiday/PL/8.0', '2011-09-20': u'TS/TS/O/1.0', '2011-09-20': u'PD/PD/LOP/7.0', 'user': u'sname', '2011-09-19': u'holiday/holiday/PL/8.0'},
u'pname': {'2011-09-21': u'TS/TS/Trickle/9.0', '2011-09-20': u'TS/dmin/POC/2.5', '2011-09-20': u'SMgr/Smgr/PL/0.5', '2011-09-20': u'SMgr/TS/Mting/2.0' 'user': u'pname', '2011-09-19': u'TechSupport/TechSupport/O/9.0'}}
one i am using below below template tag for to hash the table
Template :
@register.filter
def hash(object, attr):
gen_context = { 'object' : object }
try:
value = template.Variable('object.%s' % attr).resolve(gen_context)
except template.VariableDoesNotExist:
value = ' '
return value
HTML :
<table>
<thead>
<th>S.No</th>
<th>Name</th>
{% for dates in week_dates %}
<th>{{dates}}</th>
{% endfor %}
</thead>
<tr>
{% for fetch in data.items %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ fetch.0 }}</td>
{% for dates in week_dates %}
<td>{{ fetch.1|hash:dates }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Any help really appreciate it.
Please let me know if you need any more information.
As is explained in the tutorial AT LEAST ONCE, dictionary keys are unique. If you want to have multiple values for a key then you will have to fake it with a sequence.