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

前端 未结 4 1980
耶瑟儿~
耶瑟儿~ 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条回答
  •  感情败类
    2020-12-15 16:37

    http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

    To create a recursive relationship -- an object that has a many-to-one relationship with itself -- use models.ForeignKey('self').

    So you have it right. It's usually faster to determine if code will do what you want by running it :)

提交回复
热议问题