python SQL Union All in Django Orm

nr9pn0ug  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(106)

我目前正在做一个项目,其中我有下面描述的表,使用下面的表我想做一个查询,这将显示剩余的股票位置,所以我想在Django ORM中使用Union All,通过在查询中使用union all,我将管理我的股票位置,我已经运行了SQL查询,我也在下面描述,产生预期的结果。

class PurchaseHeader(models.Model):
    purchase_no = models.CharField(max_length=100, unique=True)
    date = models.DateField(default=datetime.date.today)
    footer_description = models.TextField()
    payment_method = models.CharField(max_length=100)
    cartage_amount = models.DecimalField(max_digits=8, decimal_places=2)
    additional_tax = models.DecimalField(max_digits=8, decimal_places=2)
    withholding_tax = models.DecimalField(max_digits=8, decimal_places=2)
    account_id = models.ForeignKey(ChartOfAccount, models.SET_NULL, blank=True, null=True)

class PurchaseDetail(models.Model):
    item_code = models.CharField(max_length=100)
    item_name = models.CharField(max_length=100)
    item_description = models.TextField()
    unit = models.CharField(max_length=100)
    quantity = models.IntegerField()
    cost_price = models.DecimalField(max_digits=8, decimal_places=2)
    retail_price = models.DecimalField(max_digits=8, decimal_places=2)
    sales_tax = models.DecimalField(max_digits=8, decimal_places=2)
    purchase_id = models.ForeignKey(PurchaseHeader, on_delete=models.CASCADE)

class PurchaseReturnHeader(models.Model):
    purchase_no = models.CharField(max_length=100, unique=True)
    date = models.DateField(default=datetime.date.today)
    footer_description = models.TextField()
    payment_method = models.CharField(max_length=100)
    cartage_amount = models.DecimalField(max_digits=8, decimal_places=2)
    additional_tax = models.DecimalField(max_digits=8, decimal_places=2)
    withholding_tax = models.DecimalField(max_digits=8, decimal_places=2)
    account_id = models.ForeignKey(ChartOfAccount, models.SET_NULL, blank=True, null=True)

class PurchaseReturnDetail(models.Model):
    item_code = models.CharField(max_length=100)
    item_name = models.CharField(max_length=100)
    item_description = models.TextField()
    unit = models.CharField(max_length=100)
    quantity = models.IntegerField()
    cost_price = models.DecimalField(max_digits=8, decimal_places=2)
    retail_price = models.DecimalField(max_digits=8, decimal_places=2)
    sales_tax = models.DecimalField(max_digits=8, decimal_places=2)
    purchase_return_id = models.ForeignKey(PurchaseReturnHeader, on_delete=models.CASCADE)

class SaleHeader(models.Model):
    sale_no = models.CharField(max_length=100, unique=True)
    date = models.DateField(default=datetime.date.today)
    footer_description = models.TextField()
    payment_method = models.CharField(max_length=100)
    cartage_amount = models.DecimalField(max_digits=8, decimal_places=2)
    additional_tax = models.DecimalField(max_digits=8, decimal_places=2)
    withholding_tax = models.DecimalField(max_digits=8, decimal_places=2)
    account_id = models.ForeignKey(ChartOfAccount, models.SET_NULL, blank=True, null=True)

class SaleDetail(models.Model):
    item_code = models.CharField(max_length=100)
    item_name = models.CharField(max_length=100)
    item_description = models.TextField()
    unit = models.CharField(max_length=100)
    quantity = models.IntegerField()
    cost_price = models.DecimalField(max_digits=8, decimal_places=2)
    retail_price = models.DecimalField(max_digits=8, decimal_places=2)
    sales_tax = models.DecimalField(max_digits=8, decimal_places=2)
    sale_id = models.ForeignKey(SaleHeader, on_delete=models.CASCADE)

class SaleReturnHeader(models.Model):
    sale_no = models.CharField(max_length=100, unique=True)
    date = models.DateField(default=datetime.date.today)
    footer_description = models.TextField()
    payment_method = models.CharField(max_length=100)
    cartage_amount = models.DecimalField(max_digits=8, decimal_places=2)
    additional_tax = models.DecimalField(max_digits=8, decimal_places=2)
    withholding_tax = models.DecimalField(max_digits=8, decimal_places=2)
    account_id = models.ForeignKey(ChartOfAccount, models.SET_NULL, blank=True, null=True,)

class SaleRetutnDetail(models.Model):
    item_code = models.CharField(max_length=100)
    item_name = models.CharField(max_length=100)
    item_description = models.TextField()
    unit = models.CharField(max_length=100)
    quantity = models.IntegerField()
    cost_price = models.DecimalField(max_digits=8, decimal_places=2)
    retail_price = models.DecimalField(max_digits=8, decimal_places=2)
    sales_tax = models.DecimalField(max_digits=8, decimal_places=2)
    sale_return_id = models.ForeignKey(SaleReturnHeader, on_delete=models.CASCADE)

我想做一个这样的查询

SELECT item_code, SUM(quantity) AS qty FROM (
    SELECT 'Purchase' AS TranType,Item_Code,Quantity FROM transaction_purchasedetail
    UNION All
    SELECT 'Purchase Return' AS TranType,Item_Code,Quantity * -1 FROM transaction_purchasereturndetail
    UNION All
    SELECT 'Sale' AS TranType,Item_Code,Quantity * -1 FROM transaction_saledetail
    UNION All
    SELECT 'Sale Return' AS TranType,Item_Code,Quantity  FROM transaction_saleretutndetail
) AS tblTemp GROUP BY Item_Code

我已经在DB Browser SQLite中运行了上面的SQL查询,并得到了预期的结果我如何在Django ORM中做到这一点。

xriantvc

xriantvc1#

https://docs.djangoproject.com/en/4.2/ref/models/querysets/#union
union有一个all kwargs:

qs.union(another_qs, all=True)

相关问题