Django:多个表模型查询

发布于 2025-01-27 11:32:21 字数 1549 浏览 1 评论 0原文

我已经在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2025-02-03 11:32:21

您可以使用 .select_redated(… )  [django-doc]检索相关billing_infosbilling Billing_infos

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

注意:django默认将添加 autofield   [django-doc] 作为priendar_key = primary_key = true使用名称ID如果自己未指定主键,则无需明确声明主键。

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.

苏璃陌 2025-02-03 11:32:21

实际上,您可以这样做:

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])

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])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文