Skip to content

Instantly share code, notes, and snippets.

@dickbrouwer
Created August 4, 2010 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dickbrouwer/508100 to your computer and use it in GitHub Desktop.
Save dickbrouwer/508100 to your computer and use it in GitHub Desktop.
qs = MyRelatedObject.objects.all()
obj_dict = dict([(obj.id, obj) for obj in qs])
objects = MyObject.objects.filter(myrelatedobj__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(obj.myobject_id, []).append(obj)
for id, related_items in relation_dict.items():
obj_dict[id].related_items = related_items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment