0

I have tried it on the Q object and in the Django ORM but could not generate the query.

class Status(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.AutoField(null=True, max_length=100)
    
    def __str__(self):
        return self.name
        
class Billing(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.AutoField(null=True, max_length=1000)
    sr_number = models.AutoField(null=True, max_length=100)
    
    def __str__(self):
        return self.name
        
class BillingInfo(models.Model):
    id = models.AutoField(primary_key=True)
    billings = models.ForeignKey(Billing, null=True, on_delete=models.SET_NULL)
    net_amount = models.AutoField(null=True, max_length=100)
    company = models.AutoField(null=True, max_length=100)
    
    def __str__(self):
        return self.company
        
class BillingOutput(models.Model):
    id = models.AutoField(primary_key=True)
    billing_infos = models.ForeignKey(BillingInfo, null=True, on_delete=models.SET_NULL)
    lbl_name = models.AutoField(null=True, max_length=100)
    lbl_qty = models.AutoField(null=True, max_length=100)
    status = models.ForeignKey(Status, null=True, on_delete=models.SET_NULL)
    
    def __str__(self):
        return self.lbl_name

I required an equivalent ORM Query for the below raw SQL query:

select bio.* from billing bil
    inner join billing_infos bi on bil.id = bi.billings_id
    inner join billing_output bio on bi.id = bio.billing_infos_id
    where bo.status_id = 1
    order by bio.id desc;

Could someone please help me with this issue?

1
  • what is bo.status_id? You did not specify a table bo. Commented May 10, 2022 at 11:03

2 Answers 2

1

You can use .select_related(…) [Django-doc] to retrieve the related billing_infos and billing of that billing_infos:

BillingOutput.objects.select_related(
    'billing_infos__billing'
).filter(status_id=1, billing_infos__billing__isnull=False).order_by('-id')

Note: Django will add by default an AutoField [Django-doc] as primary_key=True to a model with name id if you do not specify a primary key yourself, so there is no need to explicitly declare a primary key.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, @willem-van-onsem. I want to fetch billing_infos table data also. Is that possible? ` select bio.*, bi.* from billing bil inner join billing_infos bi on bil.id = bi.billings_id inner join billing_output bio on bi.id = bio.billing_infos_id where bo.status_id = 1 order by bio.id desc; `
The above query is left outer join, For an inner join what do I need to change in the ORM.
@selva316: nothing, the __isnull=False will eventually produce the same result since it filters out NULLs constructed by a LEFT OUTER JOIN. The Django ORM can sometimes optimize this slightly.
Thanks, @Willem. I want to add conditions for billing_infos.company='XXX'. Is there any other way to add a where clause for another table?
@selva316: yes, add billing_infos__company='XXX' in the filter(..) clause.
0

Actually you can do this :

qry1 = "select bio.* from billing bil
            inner join billing_infos bi on bil.id = bi.billings_id
            inner join billing_output bio on bi.id = bio.billing_infos_id
            where bo.status_id = 1
            order by bio.id desc;" 
bio = Billing.objects.raw(qry1 , [user_id])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.