Re: Customised admin saving
On Sat, Jul 11, 2009, Daniel Roseman 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 -~--~~~~--~~--~--~---
Re: Customised admin saving
Daniele Procida 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? > It seems you want to run code at save-time with access to the user's selection for a certain field. You can do this in the admin with ``ModelAdmin.save_model(...)``[1], but if you don't need access to the `request` object or direct access to the form being submitted you're better off doing something that will apply to all ``save()``s regardless of the view that displays them, like a ``pre_save`` or ``post_save`` signal. Regards, Friðrik Már [1] http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model [2] http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.pre_save --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Customised admin saving
On Jul 11, 11:37 am, "Daniele Procida" wrote: > I have a many-to-many relationship between two models, People and > Entities (enities are departments and offices etc). > > Some of these relationships are managed by the user in admin, but others > are implicit. > > For example, if the user adds John to the Laser Research Lab > Administrative Office, it means that John is also a member of the Laser > Research Lab. This is because the Entities are in a hierarchy, and being > a member of one means being a member of its parent. > > Because there will be thousands of people and hundreds of entities, I > can see that my present functions to find out what members an entity > has, or what entities someone is a member of, will involve lots of looping. > > I would rather assemble this information only when the user makes a > change to the admin, and for each person have a list of entities that > can quickly be read off, and vice-versa for the entities. > > 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? > > Thanks, > > Daniele 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. -- DR. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Customised admin saving
I have a many-to-many relationship between two models, People and Entities (enities are departments and offices etc). Some of these relationships are managed by the user in admin, but others are implicit. For example, if the user adds John to the Laser Research Lab Administrative Office, it means that John is also a member of the Laser Research Lab. This is because the Entities are in a hierarchy, and being a member of one means being a member of its parent. Because there will be thousands of people and hundreds of entities, I can see that my present functions to find out what members an entity has, or what entities someone is a member of, will involve lots of looping. I would rather assemble this information only when the user makes a change to the admin, and for each person have a list of entities that can quickly be read off, and vice-versa for the entities. 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? Thanks, 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 -~--~~~~--~~--~--~---