我有3个型号。我在date和customerproductbill上使用billmanagment 表保存产品销售记录,销售多少产品。我正在使用ORM django查询并获取 记录正确,但我还需要那些不销售的产品,在我的客户产品中只保存销售产品记录。 我需要销售而不是销售产品,因为我正在开发显示所有记录的库存分类帐。这是我的疑惑:
products = Product.objects.all().order_by('id')
---------------------------------------------------
product_sale_history = BillManagement.objects.filter(Q(creation_date__gte=from_date) &
Q(creation_date__lte=to_date) & Q(product__in=products) ).exclude(
Q(customer_id=1248)).values('customerproductbill__product_id')\
.annotate(sale_quantity = Coalesce(Sum('customerproductbill__product_qty'), 0)).order_by('customerproductbill__product_id')
class Product(models.Model):
""" Represent an Inventory Product entity """
product_name = models.CharField(max_length=512, null=True, blank=True)
reference_number = models.CharField(max_length=20, null=True, blank= True, unique=True)
class Meta:
verbose_name_plural = 'Inventory Products'
def __unicode__(self):
return self.product_name
-------------------------------------------------------
class BillManagement(models.Model):
""" Represent a Bill entity"""
bill_number = models.CharField(max_length=250, null=True, blank=True)
creation_date = models.DateField(auto_now_add=True)
product = models.ManyToManyField(Product,through='CustomerProductBill',
related_name='customer_product_bill')
class Meta:
verbose_name_plural = 'BillManagements'
ordering = ['creation_date']
def __unicode__(self):
return self.creation_date
------------------------------------------------
class CustomerProductBill(models.Model):
""" Represent a CustomerProductBill entity """
bill = models.ForeignKey(BillManagement, null=True, blank=True)
product = models.ForeignKey(Product, null=True, blank=True)
product_qty = models.IntegerField(null=False, blank=False, default=0)
class Meta:
verbose_name_plural = 'CustomerProductBill'
ordering = ['product']
def __unicode__(self):
return self.product.product_name
有人可以修改我的查询。如何使用django表格查询在表格中获得销售而非销售产品