具有来自同一个表的 2 个外键的 Django 模型 [英] Django model with 2 foreign keys from the same table
问题描述
我想要一个 Django 模型,它具有来自同一个表的 2 个外键.这是一个事件表,有 2 列供员工使用:演员"和接收者".但我得到这个错误:
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:
错误:一个或多个模型未验证:tasks.task:中介模型 TaskEvent 对 Employee 有多个外键,即模棱两可,是不允许的.
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?
我想我要添加一个 TaskEvent_to_Employee
表.其中将有两条记录,一条记录与每个 TaskEvent
相关的两名员工中的每一个.有谁知道更简单的解决方法?
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
. Does anyone know an easier workaround?
推荐答案
我还没有这样做,但我使用 inspectdb 从现有数据库生成 models.py 文件那 - 这是inspectdb退回的,所以它应该可以工作:
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屋!