Django:多个表模型查询
我已经在Q对象和Django Orm上尝试过,但无法生成查询。
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
我需要以下RAW SQL查询的等效orm查询:
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;
有人可以帮我解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
.select_redated(… )
  [django-doc]
检索相关
和billing_infos
billing
Billing_infos
:You can use
.select_related(…)
[Django-doc] to retrieve the relatedbilling_infos
andbilling
of thatbilling_infos
:实际上,您可以这样做:
Actually you can do this :