Django模型:如何引用自我? [英] Django-model: how to reference to self?

查看:89
本文介绍了Django模型:如何引用自我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有类 Employee 。我想要有一个引用同一个类的不同实例的字段。

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 following code?

ref_employee= models.ForeignKey('self',null=True,blank=True)


推荐答案

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


创建一个递归关系 -
一个与自己有多对一
关系的对象 - 使用
models.ForeignKey('self')

你说得对。通过运行它来确定代码是否能够执行所需的速度通常更快:)

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

这篇关于Django模型:如何引用自我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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