Django中OneToOneField与ForeignKey有何本质区别?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2163个文字,预计阅读时间需要9分钟。
根植于Django官方文档的介绍:这是一种一对一的关系。从概念上看,这与带有 unique=True 的外键类似,但关系的反向一侧将直接返回一个单个对象。OneToOneField 在 ForeignKey 上增加了 `unique=True`。
根据Django官方文档介绍:
A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object.
OneToOneField与ForeignKey加上unique=True效果基本一样,但是用OneToOneField反向关联会直接返回对象。
相反地,使用ForeignKey, 反向关联后会返回QuerySet。
本文共计2163个文字,预计阅读时间需要9分钟。
根植于Django官方文档的介绍:这是一种一对一的关系。从概念上看,这与带有 unique=True 的外键类似,但关系的反向一侧将直接返回一个单个对象。OneToOneField 在 ForeignKey 上增加了 `unique=True`。
根据Django官方文档介绍:
A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object.
OneToOneField与ForeignKey加上unique=True效果基本一样,但是用OneToOneField反向关联会直接返回对象。
相反地,使用ForeignKey, 反向关联后会返回QuerySet。

