本文介绍了可以将Django中的模型类型连接到一个对象中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有两个模型对象Person和Address。地址有个人id的引用。一个查询看起来像是将它们作为一个对象一起拖出来,还是不可能用Django?解决方案
阅读,然后可以使用 person.address
获取地址。
For example I have two model objects, Person and Address. Address has a reference to a Person id. What would a query look like that pulls them out together as one object, or is that not possible to do with Django?
解决方案
Have a read of the Django docs on related objects. Going from a Person to related Addresses is equivalent to going from a Blog to its related Entries in the examples.
If you have a person, you can do person.address_set.all()
to get all addresses for that person.
If each person has only one address, use a OneToOneField
, and then you can use person.address
to get the address.
这篇关于可以将Django中的模型类型连接到一个对象中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!