Here is the code

query = 'SELECT  policy_agreement.*, policy.* from policy_agreement INNER JOIN policy ON policy_agreement.policy_id=policy.id WHERE policy.policy_parent_id IN (12,16,18,46) AND status=1'
policy_agreements = list(PolicyAgreement.objects.raw(query))

I'm getting the columns only in policy_agreement table, not getting columns of policy table.

Thanks in advance.

2 Answers

0
Community On

To get the related objects append .select_related("policy")

First you do like this:

q=PolicyAgreement.objects.select_related("policy")

OR

q=policy.objects.select_related("PolicyAgreement")

as per your need

then print the query to see the raw query

print(q.query)
0
Jiří Brázdil On

You can do the whole query in Django ORM. The double underscore __ is used to access related fields' properties in DB calls.

qs = PolicyAgreement.objects.select_related("policy")
qs = qs.filter(policy__policy_parent_id__in=(12,16,18,46), status=1)