Django-不允许使用具有相同外键类型的属性,如何解决呢?

时间:2018-07-29 03:02:34

标签: python django model foreign-keys

在我的Django模型中,我的模型具有3个具有相同外键类型的属性。

Fri Jul 27 2018 20:05:22 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token >
   at Module._compile (module.js:434:25)
   at Object..js (module.js:464:10)
   at Module.load (module.js:353:31)
   at Function._load (module.js:311:12)
   at Module.require (module.js:359:17)
   at require (module.js:375:17)
   at Object.<anonymous> (D:\Program Files\iisnode\interceptor.js:459:1)
   at Module._compile (module.js:446:26)
   at Object..js (module.js:464:10)
   at Module.load (module.js:353:31)

但是,它在进行“ makemigrations”之后对此有所抱怨。错误如下:

    employee = models.ForeignKey(Employee)
    approved_by = models.ForeignKey(Employee)
    created_by = models.ForeignKey(Employee)

models.py

SystemCheckError: System check identified some issues:

ERRORS:
railercomapp.Leave.approved_by: (fields.E304) Reverse accessor for 'Leave.approved_by' clashes with reverse accessor for 'Leave.created_by'.
    HINT: Add or change a related_name argument to the definition for 'Leave.approved_by' or 'Leave.created_by'.
railercomapp.Leave.approved_by: (fields.E304) Reverse accessor for 'Leave.approved_by' clashes with reverse accessor for 'Leave.employee'.
    HINT: Add or change a related_name argument to the definition for 'Leave.approved_by' or 'Leave.employee'.
railercomapp.Leave.created_by: (fields.E304) Reverse accessor for 'Leave.created_by' clashes with reverse accessor for 'Leave.approved_by'.
    HINT: Add or change a related_name argument to the definition for 'Leave.created_by' or 'Leave.approved_by'.
railercomapp.Leave.created_by: (fields.E304) Reverse accessor for 'Leave.created_by' clashes with reverse accessor for 'Leave.employee'.
    HINT: Add or change a related_name argument to the definition for 'Leave.created_by' or 'Leave.employee'.
railercomapp.Leave.employee: (fields.E304) Reverse accessor for 'Leave.employee' clashes with reverse accessor for 'Leave.approved_by'.
    HINT: Add or change a related_name argument to the definition for 'Leave.employee' or 'Leave.approved_by'.
railercomapp.Leave.employee: (fields.E304) Reverse accessor for 'Leave.employee' clashes with reverse accessor for 'Leave.created_by'.
    HINT: Add or change a related_name argument to the definition for 'Leave.employee' or 'Leave.created_by'.

2 个答案:

答案 0 :(得分:4)

您是否已阅读错误消息中的“提示”?在字段中提供related_name自变量。这样的事情应该起作用。

employee = models.ForeignKey(Employee, related_name='leaves')
approved_by = models.ForeignKey(Employee, related_name='approved_leaves')
created_by = models.ForeignKey(Employee, related_name='created_leaves')

答案 1 :(得分:0)

您有许多外键,Django无法为其生成唯一的名称。

您可以通过在模型中的外键字段定义中添加“ related_name”自变量来提供帮助,

class Leave(models.Model):
    leavedate = models.DateField()
    employee = models.ForeignKey(Employee, related_name='leave_employee')
    company = models.ForeignKey(Company)
    reason = models.IntegerField(default=0)
    description = models.CharField(max_length=100)
    status = models.IntegerField(default=0)
    approval_note = models.CharField(max_length=100)
    approved_by = models.ForeignKey(Employee, related_name='leave_approved_by')
    created_by = models.ForeignKey(Employee, related_name='created_by')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

related_name在什么地方有用?
related_name属性指定从 Employee 模型回到您的 Leave 模型的反向关系的名称。

如果您未指定related_name,则Django会自动使用模型名称后缀_set创建一个,例如Employee.leave_set.all()。当出现多个FK关系时,反向关系变得big昧。

假设如果要获取与特定PK值为1的Employee对应的Leave的所有详细信息,它将为as

emp = Employee.objects.get(id=1)
emp.leave_employee.all()

这等同于,

Leave.objects.filter(employee=1)