如何使用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,db_column='idtrp_rel')
    cust_vendor_rel = models.CharField(max_length=50,blank=True,null=True)
    sender_id_rel = models.CharField(max_length=50,null=True)
    old_vendor_rel = models.CharField(max_length=50,null=True)
    vendor_name_rel = models.CharField(max_length=50,null=True)
    category_rel = models.CharField(max_length=50,null=True)

class AppTradingPartnerTrp(models.Model):
    id_trp = models.AutoField(primary_key=True)
    tpid_trp = models.CharField(max_length=50,null=True)
    name_trp = models.CharField(max_length=50)
    description_trp = models.CharField(max_length=100,null=True)
    idtrn_trp = models.ForeignKey('AppTransmissionTrn',db_column='idtrn_trp',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,null=True)
    address_1_cst = models.CharField(max_length=50,null=True)
    address_2_cst = models.CharField(max_length=50,null=True)
    address_3_cst = models.CharField(max_length=50,null=True)
    city_cst = models.CharField(max_length=50,null=True)
    state_cst = models.CharField(max_length=50,null=True)
    zip_cst = models.CharField(max_length=10,null=True)
    country_cst = models.CharField(max_length=50,null=True)
    salesrep_cst = models.CharField(max_length=50,null=True)
    type_cst = models.CharField(max_length=10,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,null=True)

我正在尝试使用“ AppCustomerTpRel”表中的值。

我尝试了app_customer_tp_rel.id_relapp_customer_tp_rel__id_relsql表名,也尝试了使用模型名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',...
)