How to access related values with select_related()

951 views Asked by At

I have a QuerySet that I'm attempting to work with the values on related tables. I see the related tables/values when I run the queryset.query, however I'm not sure how to pull those values for use in forms/tables.

Here is my QuerySet Object:

tpList = AppCustomerTpRel.objects.filter(idcst_rel=selection).select_related('idcst_rel', 'idtrp_rel').values()

Here are the related Models.

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)

I am trying to work with values from the 'AppCustomerTpRel' Table.

I have tried app_customer_tp_rel.id_rel as well as app_customer_tp_rel__id_rel with the SQL Table Names, I have also tried both using the Model Name AppCustomerTpRel

TIA!

1

There are 1 answers

1
weAreStarsDust On BEST ANSWER

Just wright all related fields the you need in 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',
    ...
)