可以将Django中的模型类型连接到一个对象中吗? [英] Is it possible to join model types in Django into one object?
问题描述
阅读相关对象上的Django文档。从个人到相关地址相当于从博客到其相关条目的示例。
如果你有一个人,你可以做 person.address_set.all()
以获取该人的所有地址。
如果每个人只有一个地址,请使用 OneToOneField
,然后可以使用 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中的模型类型连接到一个对象中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!