def get_context_data(self, **kwargs):
context = super(ChartIconMixin, self).get_context_data(**kwargs)
# Get all the template icons and them in a dict we can use as a
# lookup
icons = Template.objects.defer('data')
icon_lookup = {i.title: i.icon for i in icons}
default_icon = static('chartwerk/img/chartwerk_100.png')
for chart in context[self.context_object_name]:
tpl_title = chart.data['template']['title']
# If the chart template isn't found or it's icon field is null,
# use default
if not icon_lookup.get(tpl_title):
chart.icon = default_icon
else:
chart.icon = icon_lookup[tpl_title].url
return context
评论列表
文章目录