如何使用 select_related() 访问相关值
How to access related values with select_related()
我有一个 QuerySet,我正在尝试使用相关表上的值。当我 运行 queryset.query
时,我看到了相关的 tables/values,但是我不确定如何提取这些值以用于 forms/tables.
这是我的 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' Table.
中的值
我已经尝试使用 app_customer_tp_rel.id_rel
和 app_customer_tp_rel__id_rel
SQL Table 名称,我也尝试过使用模型名称 AppCustomerTpRel
TIA!
只需在 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',
...
)
我有一个 QuerySet,我正在尝试使用相关表上的值。当我 运行 queryset.query
时,我看到了相关的 tables/values,但是我不确定如何提取这些值以用于 forms/tables.
这是我的 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' Table.
中的值我已经尝试使用 app_customer_tp_rel.id_rel
和 app_customer_tp_rel__id_rel
SQL Table 名称,我也尝试过使用模型名称 AppCustomerTpRel
TIA!
只需在 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',
...
)