2011-07-28 50 views
3
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其中事件=事件的Django的queryset問題

+0

如果你看看'Ticket',它有一個'cartitem_set'屬性,它可以提供引用票證的所有'CartItems'列表。由於可能有不止一個,所以沒有簡單的票價時間數量。可能有多個數量。 –

+0

@Nai下面的答案對你有幫助嗎? –

回答

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 

這應該給你一個事件的總收入。