I had the same issue a few months ago. My only answer to you is that is not worth to develop this all for self. There is a great django library called django-mptt<http://github.com/django-mptt/django-mptt> that does all the heavy lifting for you. It's very fast, has many helper functions and even admin integration! I suggest that you check it out.
Regards, Cesar Canassa 2010/10/6 Paweł Roman <romapa...@googlemail.com> > Yuck, in_bulk() is useless! I thought it would return a nice > dictionary just as expected, but instead it makes me deliver a list of > keys first. Why? > > Eventually I wrote something more usable: > > mydict = {} > for model in MyModel.objects.all().iterator(): > mydict[model.id]=model > > Correct me if I'm wrong but this is hundred times more useful than > current in_bulk() implementation. > > > On Oct 6, 5:31 pm, Paweł Roman <romapa...@googlemail.com> wrote: > > OK, I've got it. It's in_bulk() :) Nevermind the question. > > > > On Oct 6, 5:27 pm, Paweł Roman <romapa...@googlemail.com> wrote: > > > > > I have a model which has a tree-like structure (not exactly a FK on > > > self but we can assume that's the case). > > > > > I want to visualise this tree using all records from the db. It seems > > > impossible to do with QuerySet, if I have say 100 records (100 nodes > > > in the tree) and I try building the tree recursively django will > > > execute a SELECT for each node so it will hit the db 100 times to > > > build the tree. > > > > > I thought I could pull the data in one query using objects.all() and > > > then build the whole tree by iterating objects in memory. But again, > > > the structures returned by QuerySet are not the best for this purpose. > > > I can get either list of dictionaries or list of tuples. No > > > dictionary! So, to get a list of child nodes for each node I'd have to > > > iterate thru the whole collection which is not efficient at all. I > > > can't find a way to immediately lookup already fetched records by id! > > > > > Am I missing something? Iterating tree-like data structures seems > > > something quite common problem and someone must have done it already > > > with django. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.