class Ticket(models.Model):
event = models.ForeignKey(Event)
name = models.CharField('Name', max_length=255)
price = models.FloatField('Price', blank=True)
class CartItem(models.Model):
cart = models.ForeignKey(Cart)
ticket = models.ForeignKey(Ticket)
quantity = models.IntegerField()
如何获取Ticket.price * CartItem.Quantity where event = event
答案 0 :(得分:4)
您需要添加错误检查,但您可以按照逻辑顺序执行此操作:
total = 0
cart_items = CartItem.objects.filter(ticket__event=event) # assuming there are multiple cart items per event
for cart_item in cart_items:
new_total = cart_item.ticket.price * cart_item.quantity
total = total + new_total
这应该为您提供活动的总收入。