code: def queryset_to_dict(qs, key='pk', singular=True):
      
      if singular:
          result = {}
          for u in qs:
              result.setdefault(getattr(u, key), u)
      else:
          result = defaultdict(list)
          for u in qs:
              result[getattr(u, key)].append(u)
      return result
docs: Given a queryset will transform it into a dictionary based on ``key``.