Python 如何从Django中的查询集中获取所有相关字段?
我有两个模型,Python 如何从Django中的查询集中获取所有相关字段?,python,django,django-queryset,Python,Django,Django Queryset,我有两个模型,Session和SessionType,它们具有多对一的关系。在会话上还有一个系列外键,如下所示: from django.db import models class SesssionType(models.Model): pass class Session(models.Model): session_type = models.ForeignKey('SessionType') family = models.ForeignKey('Fami
Session
和SessionType
,它们具有多对一的关系。在会话
上还有一个系列
外键,如下所示:
from django.db import models
class SesssionType(models.Model):
pass
class Session(models.Model):
session_type = models.ForeignKey('SessionType')
family = models.ForeignKey('Family')
ipdb> SessionType.objects.filter(session__family=family).order_by('session__session_number')
<QuerySet [<SessionType: Welcome>, <SessionType: First-Time Parents: The Basics of Birth>, <SessionType: Initial Postpartum Lactation>, <SessionType: Sleep Techniques for New Babies>, <SessionType: Breastfeeding Preparation>, <SessionType: Newborn Care Basics>, <SessionType: Easing the Transition Back to Work>, <SessionType: Preparing for Parenting>, <SessionType: Decoding Baby Cues>, <SessionType: Postpartum Doula Support>, <SessionType: First-Time Parents: Birth Prep Q&A>, <SessionType: Postpartum Lactation Follow-Up>, <SessionType: Sleep Training for 4 Months & Beyond>, <SessionType: Mental Wellness in Pregnancy>, <SessionType: Infant CPR>, <SessionType: Prenatal Pelvic Physical Therapy>, <SessionType: Prenatal Massage>]>
对于family
的某个实例family
,我在Session\u集中有几个Session
对象:
ipdb> family.session_set.all()
<QuerySet [<Session: Welcome>, <Session: Breastfeeding Preparation - Timothy Anderson>, <Session: First-Time Parents: The Basics of Birth>, <Session: Initial Postpartum Lactation>, <Session: Sleep Techniques for New Babies>, <Session: Breastfeeding Preparation>, <Session: Newborn Care Basics>, <Session: Easing the Transition Back to Work>, <Session: Preparing for Parenting>, <Session: Decoding Baby Cues>, <Session: Postpartum Doula Support>, <Session: First-Time Parents: Birth Prep Q&A>, <Session: Postpartum Lactation Follow-Up>, <Session: Sleep Training for 4 Months & Beyond>, <Session: Mental Wellness in Pregnancy>, <Session: Infant CPR>, <Session: Prenatal Pelvic Physical Therapy>, <Session: Prenatal Massage>]>
该方法似乎就是为了实现这一点,但调用它并不会产生期望的结果:
ipdb> family.session_set.select_related('session_type')
<QuerySet [<Session: Welcome>, <Session: Breastfeeding Preparation - Timothy Anderson>, <Session: First-Time Parents: The Basics of Birth>, <Session: Initial Postpartum Lactation>, <Session: Sleep Techniques for New Babies>, <Session: Breastfeeding Preparation>, <Session: Newborn Care Basics>, <Session: Easing the Transition Back to Work>, <Session: Preparing for Parenting>, <Session: Decoding Baby Cues>, <Session: Postpartum Doula Support>, <Session: First-Time Parents: Birth Prep Q&A>, <Session: Postpartum Lactation Follow-Up>, <Session: Sleep Training for 4 Months & Beyond>, <Session: Mental Wellness in Pregnancy>, <Session: Infant CPR>, <Session: Prenatal Pelvic Physical Therapy>, <Session: Prenatal Massage>]>
ipdb>family.session\u set.选择相关的('session\u type'))
如上所述,调用select_related()
生成了一个包含Session
对象的查询集,而不是SessionType
对象。如何获取SessionType
对象?我通过过滤所有SessionType
对象解决了这个问题,如下所示:
from django.db import models
class SesssionType(models.Model):
pass
class Session(models.Model):
session_type = models.ForeignKey('SessionType')
family = models.ForeignKey('Family')
ipdb> SessionType.objects.filter(session__family=family).order_by('session__session_number')
<QuerySet [<SessionType: Welcome>, <SessionType: First-Time Parents: The Basics of Birth>, <SessionType: Initial Postpartum Lactation>, <SessionType: Sleep Techniques for New Babies>, <SessionType: Breastfeeding Preparation>, <SessionType: Newborn Care Basics>, <SessionType: Easing the Transition Back to Work>, <SessionType: Preparing for Parenting>, <SessionType: Decoding Baby Cues>, <SessionType: Postpartum Doula Support>, <SessionType: First-Time Parents: Birth Prep Q&A>, <SessionType: Postpartum Lactation Follow-Up>, <SessionType: Sleep Training for 4 Months & Beyond>, <SessionType: Mental Wellness in Pregnancy>, <SessionType: Infant CPR>, <SessionType: Prenatal Pelvic Physical Therapy>, <SessionType: Prenatal Massage>]>
ipdb>SessionType.objects.filter(session\uu family=family)。排序依据('session\uu session\u number'))
但是,我有一点感觉,这比仅从系列.session\u集合
中的session
对象中获取相应的session\u类型
效率低,因此解释了为什么选择相关()
未按预期工作,我们仍然非常欢迎。正如您所说,您可以使用新的查询集选择所有会话类型
对象
但是select\u-related
用法不同
根据Django文档和我使用的所有与select_相关的not change queryset对象类型。但只能通过一次查询从数据库中选择所有相关对象。例如,请参见此查询:
for item in family.session_set.select_related('session_type').all():
print item.session_type
单击数据库一次,但在编写此命令时:
for item in family.session_set.all():
print item.session_type
对于每次打印,都会发生一次数据库命中和一次基本查询的数据库命中。数据太少并不重要,但是当你的数据太大的时候,你的网站就慢了,没有选择相关的。但是要注意使用它。如果你使用它太多的行动是相反的,你的网站去慢
查看更多关于