【发布时间】:2011-03-09 19:18:08
【问题描述】:
我使用的是 Django/Python,但这里绝对可以接受伪代码。
使用一些已经存在的模型,我有Employees,每个模型都有一个Supervisor,这本质上是与另一个Employee 的外键类型关系。
员工/主管层次结构是这样的:
任何给定的员工都有一名主管。该主管可能有一名或多名“下属”员工,并且也有他/她自己的主管。检索我的“上线”应该返回我的主管、他的主管、她的主管等,直到到达没有主管的员工。
由于这是一个现有的代码库和项目,我想知道“pythonic”或实现以下功能的正确方法:
def get_upline(employee):
# Get a flat list of Employee objects that are
# 'supervisors' to eachother, starting with
# the given Employee.
pass
def get_downline(employee):
# Starting with the given Employee, find and
# return a flat list of all other Employees
# that are "below".
pass
我觉得使用 Django ORM 可能有一种简单的方法可以做到这一点,但如果没有,我会接受任何建议。
我还没有彻底检查过 Django-MPTT,但如果我可以保留模型并简单地获得更多功能,那将是值得的。
【问题讨论】:
标签: python django parent-child hierarchy