可以将Django中的模型类型连接到一个对象中吗? [英] Is it possible to join model types in Django into one object?

查看:85
本文介绍了可以将Django中的模型类型连接到一个对象中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有两个模型对象Person和Address。地址有个人id的引用。一个查询看起来像是将它们作为一个对象一起拖出来,还是不可能用Django?

解决方案

阅读相关对象上的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屋!

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