简体   繁体   English

如何在Django中创建一个分层组织(像树一样)?

[英]How to create a hierarchical organization (tree like) in django?

I have to create an organization structure (tree like) in django. 我必须在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. 当前,我正在使用auth_user具有parent_id的一对一映射表,但是当组织级别更高时,我必须在同一表上触发多个查询以获取顶级节点的子代。

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. 我认为如果可以重写has_perm方法来执行相同操作会更好。

Is there a better way to do this or a plugin which does similar thing? 有没有更好的方法或类似的插件?

You can solve your organization structure parent/ancestor querying problems by using one of several known approaches (see, eg, 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. 对于特定于Django的问题,有一个相当活跃的项目叫做treebeard ,它提供了邻接表,物化路径和嵌套集。

For the permission issue, you'd have to specify the problem in more detail, but one possibility if you use treebeard is: 对于权限问题,您必须更详细地说明问题,但是如果您使用treebeard则一种可能是:

def can_view(entry, user):
    return entry.user.get_root() == user.get_root()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM