如何在 Django 中相交两个查询集?
How to intersect two query sets in django?
我有两个问题。其中一张有已售出的商品id和销售金额,另一张有商品id和商品价格。
query_product = Model1.objects.filter(...).values_list('ProductID', 'ProductPrice')
query_sale = Model2.objects.filter(...).values_list('SaleProductID', 'ProductAmount')
我想计算 IF SaleProductID = ProductID, Sum(F('ProductPrice')*F('ProductAmount')。但是,我无法通过查询找到如何做到这一点。注意:Model2有 Model1 的外键。
你有什么建议吗?谢谢!
编辑:我想在 PBGelenSiparis class 的管理站点上显示总量。这是翻译后的完整模型;
class PBUrun(models.Model):
PBUrunNu = models.IntegerField(verbose_name='Product Code')
PBUrunName = models.CharField(verbose_name='Product Name', max_length=200)
PBUrunPrice = models.DecimalField(verbose_name='Product Price', max_digits=10, decimal_places=2)
class PBGelenSiparis(models.Model):
pburun = models.ForeignKey(PBUrun, verbose_name='Choose Product', on_delete=models.CASCADE )
PBGelSipNu = models.AutoField(verbose_name='Order Number', primary_key = True)
def GelenSiparisTutari(self):
find_ordered_products = PBGelenSiparisUrun.objects.filter(pbgelensiparis_id = self.PBGelSipNu)\
.values('pburun_id')
order_product_price = PBUrun.objects.filter(PBUrunNu__in = siparis_urunlerini_bul).values_list('PBUrunNu','PBUrunFiyat')
order_product_amount = PBGelenSiparisUrun.objects.filter(pbgelensiparis_id = self.PBGelSipABNu).values_list('pburun_id','PBGelSipUrunAdet')
class PBGelenSiparisUrun(models.Model):
pburun = models.ForeignKey(PBUrun, verbose_name='Choose Product', on_delete=models.CASCADE )
pbgelensiparis = models.ForeignKey(PBGelenSiparis, verbose_name='Order', on_delete=models.CASCADE)
PBGelSipUrunAdet = models.IntegerField(verbose_name='Order Quantity')
这可能有帮助:
from django.db.models import Subquery, IntegerField
class SQSum(Subquery):
output_field = IntegerField()
template = "(SELECT sum(PBGelSipUrunAdet) from (%(subquery)s) _sum)"
accumulated_quantity_subquery = SQSum(
PBGelenSiparisUrun.objects.filter(
pburun=OuterRef('id')
).values("PBGelSipUrunAdet")
)
queryset = PBUrun.objects.annotate(
total_quantity=accumulated_quantity_subquery,
amount=F("PBUrunPrice") * F("total_quantity")
)
这将为每个产品注释数量。
我有两个问题。其中一张有已售出的商品id和销售金额,另一张有商品id和商品价格。
query_product = Model1.objects.filter(...).values_list('ProductID', 'ProductPrice')
query_sale = Model2.objects.filter(...).values_list('SaleProductID', 'ProductAmount')
我想计算 IF SaleProductID = ProductID, Sum(F('ProductPrice')*F('ProductAmount')。但是,我无法通过查询找到如何做到这一点。注意:Model2有 Model1 的外键。
你有什么建议吗?谢谢!
编辑:我想在 PBGelenSiparis class 的管理站点上显示总量。这是翻译后的完整模型;
class PBUrun(models.Model):
PBUrunNu = models.IntegerField(verbose_name='Product Code')
PBUrunName = models.CharField(verbose_name='Product Name', max_length=200)
PBUrunPrice = models.DecimalField(verbose_name='Product Price', max_digits=10, decimal_places=2)
class PBGelenSiparis(models.Model):
pburun = models.ForeignKey(PBUrun, verbose_name='Choose Product', on_delete=models.CASCADE )
PBGelSipNu = models.AutoField(verbose_name='Order Number', primary_key = True)
def GelenSiparisTutari(self):
find_ordered_products = PBGelenSiparisUrun.objects.filter(pbgelensiparis_id = self.PBGelSipNu)\
.values('pburun_id')
order_product_price = PBUrun.objects.filter(PBUrunNu__in = siparis_urunlerini_bul).values_list('PBUrunNu','PBUrunFiyat')
order_product_amount = PBGelenSiparisUrun.objects.filter(pbgelensiparis_id = self.PBGelSipABNu).values_list('pburun_id','PBGelSipUrunAdet')
class PBGelenSiparisUrun(models.Model):
pburun = models.ForeignKey(PBUrun, verbose_name='Choose Product', on_delete=models.CASCADE )
pbgelensiparis = models.ForeignKey(PBGelenSiparis, verbose_name='Order', on_delete=models.CASCADE)
PBGelSipUrunAdet = models.IntegerField(verbose_name='Order Quantity')
这可能有帮助:
from django.db.models import Subquery, IntegerField
class SQSum(Subquery):
output_field = IntegerField()
template = "(SELECT sum(PBGelSipUrunAdet) from (%(subquery)s) _sum)"
accumulated_quantity_subquery = SQSum(
PBGelenSiparisUrun.objects.filter(
pburun=OuterRef('id')
).values("PBGelSipUrunAdet")
)
queryset = PBUrun.objects.annotate(
total_quantity=accumulated_quantity_subquery,
amount=F("PBUrunPrice") * F("total_quantity")
)
这将为每个产品注释数量。