具有来自同一个表的2个外键的Django模型 [英] Django model with 2 foreign keys from the same table

查看:182
本文介绍了具有来自同一个表的2个外键的Django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个来自同一个表的两个外键的Django模型。这是一个事件表,其中有员工2列:actor和receiver。但是我收到这个错误:


错误:一个或多个模型未验证:tasks.task:中介
模型TaskEvent多于一个雇员的外键,这是
含糊不清,不允许。


有更好的模式这个?
谢谢



我想我要添加一个 TaskEvent_to_Employee 表。将有两个记录,其中一个与每个 TaskEvent 相关的两个员工中的每一个。任何人知道一个更容易的解决方法?

解决方案

我还没有这样做,但我使用 inspectdb 从现有的数据库中生成models.py文件,这正是这样 - 这是inspectdb扔回来的,所以它应该可以工作:

  creator = models.ForeignKey(Users,null = True,related_name ='creator')
assignee = models.ForeignKey(Users,null = True,related_name ='assignee')

希望对您有用 - 如果我也不会有问题。


I wanted a Django model with 2 foreign keys from the same table. It's an event table which has 2 columns for employees: the 'actor' and the 'receiver'. But I get this error:

Error: One or more models did not validate: tasks.task: Intermediary model TaskEvent has more than one foreign key to Employee, which is ambiguous and is not permitted.

Is there a better way to model this? Thanks

I think I'm going to add a TaskEvent_to_Employee table. There will be two records in it, one for each of the two employees related to each TaskEvent. Anyone know an easier workaround?

解决方案

I haven't done this yet, but I used inspectdb to generate the models.py file from an existing DB that does exactly that - this is what inspectdb threw back, so it should work:

creator = models.ForeignKey(Users, null=True, related_name='creator')
assignee = models.ForeignKey(Users, null=True, related_name='assignee')

Hope that works for you - if it doesn't I am going to have a problem too.

这篇关于具有来自同一个表的2个外键的Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆