开发者

Django queryset question

开发者 https://www.devze.com 2023-03-23 19:28 出处:网络
class Ticket(models.Model): event = models.ForeignKey(Event) name = models.CharField(\'Name\', max_length=255)
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()

How do I get Ticket.price * CartItem.Quantity where event = event


You'll need to add error checking but you could do something like this logic-wise:

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

That should give you total revenue for an event.

0

精彩评论

暂无评论...
验证码 换一张
取 消