In Django, how do you make a model refer to itself?

前端 未结 4 1989
耶瑟儿~
耶瑟儿~ 2020-12-15 15:49

Assume we have class Employee. I want to have a field which references a different instance of the same class.

How to write this? How about the followi

4条回答
  •  旧时难觅i
    2020-12-15 16:18

    You can reference other models by name (using a string, including package), instead of by the class directly:

    So, if your Employee class is in the hr app:

    class Employee(models.model):
       other_employee = models.ForeignKey('hr.models.Employee', null=True, blank=True)
    

提交回复
热议问题