On Sat, Jul 11, 2009, Daniel Roseman <dan...@roseman.org.uk> wrote: >> I think this means that I need to add a routine to my admin.py so that >> when the user hits Save, it will also manually assemble the information >> and save it for the affected models. >> >> Is that correct? If so, what is doing this called, so I can look it up >> in the documentation?
>Well, this is called a Signal, and you wouldn't do it in admin.py >necessarily - you would connect a function to the post-save signal so >that any time the user is saved, the group information is also >collected and saved. > >However I think a better solution for you would be to investigate >django-mptt. MPTT is an algorithm that allows you to efficiently >retrieve hierarchical data, so that in your example you would be able >to get the user's group and all of its parent groups in a single query >rather than having to denormalise the data and store it per user. I already use MPTT to manage the hierarchy, and in fact my functions use MPTT's get_ancestors(). But it's complicated - because a person can be a member of more than one entity, and because a person also has a home_entity, in order to get a clean list (no duplicates) of the entities they belong to I have a function called gather_entities(): def gather_entities(self): entitylist = [self.home_entity] entitylist.extend(self.home_entity.get_ancestors()) for entity in self.entities.all(): entitylist.append(entity) entitylist.extend(entity.get_ancestors()) entitylist.sort() last = entitylist[-1] for i in range(len(entitylist)-2, -1, -1): if last==entitylist[i]: del entitylist[i] else: last=entitylist[i] return entitylist That's not too bad, but to go the opposite way, and to find a list of members from a given entity will require a lot of poking around, it seems to me. Daniele --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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 -~----------~----~----~----~------~----~------~--~---