django - 如何使用 select_related() 访问相关值
问题描述
我有一个 QuerySet,我正在尝试使用相关表上的值。我在运行时看到了相关的表/值queryset.query
,但是我不确定如何提取这些值以在表单/表中使用。
这是我的 QuerySet 对象:
tpList = AppCustomerTpRel.objects.filter(idcst_rel=selection).select_related('idcst_rel', 'idtrp_rel').values()
这是相关的模型。
class AppCustomerTpRel(models.Model):
id_rel = models.AutoField(primary_key=True)
idcst_rel = models.ForeignKey(AppCustomerCst, models.DO_NOTHING, db_column='idcst_rel')
idtrp_rel = models.ForeignKey(AppTradingPartnerTrp, models.DO_NOTHING, db_column='idtrp_rel')
cust_vendor_rel = models.CharField(max_length=50, blank=True, null=True)
sender_id_rel = models.CharField(max_length=50, blank=True, null=True)
old_vendor_rel = models.CharField(max_length=50, blank=True, null=True)
vendor_name_rel = models.CharField(max_length=50, blank=True, null=True)
category_rel = models.CharField(max_length=50, blank=True, null=True)
class AppTradingPartnerTrp(models.Model):
id_trp = models.AutoField(primary_key=True)
tpid_trp = models.CharField(max_length=50, blank=True, null=True)
name_trp = models.CharField(max_length=50)
description_trp = models.CharField(max_length=100, blank=True, null=True)
idtrn_trp = models.ForeignKey('AppTransmissionTrn', models.DO_NOTHING, db_column='idtrn_trp', blank=True, null=True)
class AppCustomerCst(models.Model):
id_cst = models.AutoField(primary_key=True)
is_active_cst = models.BooleanField()
name_cst = models.CharField(max_length=50, blank=True, null=True)
address_1_cst = models.CharField(max_length=50, blank=True, null=True)
address_2_cst = models.CharField(max_length=50, blank=True, null=True)
address_3_cst = models.CharField(max_length=50, blank=True, null=True)
city_cst = models.CharField(max_length=50, blank=True, null=True)
state_cst = models.CharField(max_length=50, blank=True, null=True)
zip_cst = models.CharField(max_length=10, blank=True, null=True)
country_cst = models.CharField(max_length=50, blank=True, null=True)
salesrep_cst = models.CharField(max_length=50, blank=True, null=True)
type_cst = models.CharField(max_length=10, blank=True, null=True)
is_allowed_flat_cst = models.BooleanField()
iddef_cst = models.IntegerField()
date_created_cst = models.DateTimeField()
date_suspended_cst = models.DateTimeField(blank=True, null=True)
date_first_tran_cst = models.DateTimeField(blank=True, null=True)
date_last_tran_cst = models.DateTimeField(blank=True, null=True)
is_credit_hold_cst = models.BooleanField()
old_balance_cst = models.DecimalField(max_digits=8, decimal_places=2)
balance_notify_cst = models.DecimalField(max_digits=8, decimal_places=2)
balance_statement_cst = models.DecimalField(max_digits=8, decimal_places=2)
balance_conversion_cst = models.DecimalField(max_digits=8, decimal_places=2)
balance_cst = models.DecimalField(max_digits=8, decimal_places=2)
receive_emails_cst = models.BooleanField()
contact_domain_cst = models.CharField(max_length=100, blank=True, null=True)
我正在尝试使用“AppCustomerTpRel”表中的值。
我已经尝试app_customer_tp_rel.id_rel
过以及app_customer_tp_rel__id_rel
使用 SQL 表名称,我也尝试过使用模型名称AppCustomerTpRel
蒂亚!
解决方案
只需填写您需要的所有相关字段values()
tpList = AppCustomerTpRel.objects.filter(
idcst_rel=selection
).select_related(
'idcst_rel',
'idtrp_rel'
).values(
'idtrp_rel__id_trp',
'idtrp_rel__tpid_trp',
'idtrp_rel__name_trp',
...
)
推荐阅读
- typescript - 使用 SortableJS 对元素进行排序时防止 Mithril 创建的重复项
- html - SVG 标志 模糊
- graphql - 如何在 Strapi 中构建支持 Mongodb 聚合的自定义 graphql 查询?
- java - 如何以原始形式将文本从 docx 输入到 Android Studio?
- r - 是否有一些用于生成移位卡方分布的 R 包?
- flutter - Flutter Mobx 多个商店不更新观察者中的值
- sql - GROUP BY 案例陈述
- neo4j - 为一组节点创建每两个节点之间的关系
- docker - docker stack (swarm) 不工作但 docker-compose ok
- java - 如何反射 mybatis 映射器以在 java 8 中将不同的实体保存在一个地方