Skip to content

Instantly share code, notes, and snippets.

View dtheekshanalinux's full-sized avatar
🏠
Working from home

Dinindu Theekshana dtheekshanalinux

🏠
Working from home
  • Kurunegala, Sril lanka
View GitHub Profile
@voidnologo
voidnologo / nested_dicts.md
Last active October 3, 2021 17:54
Displaying nested dictionaries in a Django template

Recently came across a situtuation where I was trying to display a dictionary of dictionaries of lists in a django template. In the python code, the data structure was built like:

      @property
      def nested(self):
          from collections import defaultdict
          data = defaultdict(lambda: defaultdict(list))
          for x in self.a_list_of_objects:
               data['key'][x.an_attribute].append(x)