2

I have to create an organization structure (tree like) in django. Currently, I am using a one to one mapping table with auth_user having parent_id but when the level of organization is more, I have to fire multiple queries on the same table to get the children of a top level node.

Also, I need to allow permissions on several views based on that. For eg. A user can have a permission to view his own entries in a particular table and the entries added by his team. For permissions, I am currently getting the list of all the user and checking if one of the user is same as the one which added that entry and checking permission for the same(in the decorator before the view). I think it would be better if I can override has_perm method to do the same.

Is there a better way to do this or a plugin which does similar thing?

Anuj
  • 1,160
  • 2
  • 20
  • 40
  • 3
    There are some django exts like django-mptt http://imom0.github.io/2014/more-efficient-way-for-handling-hierarchical-structure-in-relational-database.html – iMom0 Mar 05 '14 at 04:26
  • django-mptt is not working from me as I have the parent_id in another table which has one to one mapping with auth_user but not in auth_user. – Anuj Mar 05 '14 at 08:32

1 Answers1

0

You can solve your organization structure parent/ancestor querying problems by using one of several known approaches (see, e.g., this question for details). For Django-specific concerns, there's a reasonably active project called treebeard that offers adjacency lists, materialized path, and nested sets.

For the permission issue, you'd have to specify the problem in more detail, but one possibility if you use treebeard is:

def can_view(entry, user):
    return entry.user.get_root() == user.get_root()
Community
  • 1
  • 1
Felipe
  • 3,003
  • 2
  • 26
  • 44