Python Django ORM用客户分数标注产品
考虑以下Django模型Python Django ORM用客户分数标注产品,python,django,orm,annotate,Python,Django,Orm,Annotate,考虑以下Django模型 Customer(models.Model) name = models.CharField() Product(models.Model) name = models.CharField() Score(models.Model) points = models.IntegerField() customer = models.ForeignKey(Customer) product = models.ForeignKey(
Customer(models.Model)
name = models.CharField()
Product(models.Model)
name = models.CharField()
Score(models.Model)
points = models.IntegerField()
customer = models.ForeignKey(Customer)
product = models.ForeignKey(Product)
我想得到一份所有产品的清单,上面注明:
- 所有客户的总分
- 仅来自当前客户的分数
products = Product.objects.annotate(total_points = Sum('score__points')).all()
cus = Cusomter.objects.get(pk=123)
products = Product.objects.annotate(total_points = Sum('score__points'),\
current_customer_points= (Score.points where customer=cus and product = this).all()
但是我不知道如何添加一个只包含当前客户分数的注释(如果客户已经查看了产品,则没有其他注释)。我想要这样的东西:
products = Product.objects.annotate(total_points = Sum('score__points')).all()
cus = Cusomter.objects.get(pk=123)
products = Product.objects.annotate(total_points = Sum('score__points'),\
current_customer_points= (Score.points where customer=cus and product = this).all()