首页 > 解决方案 > 如何使用 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

蒂亚!

标签: djangodjango-select-related

解决方案


只需填写您需要的所有相关字段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',
    ...
)

推荐阅读