2012-08-29 146 views
0

我正在建立一個食品配送網站。我目前已經完成了所有設置,但是我正在構建管理頁面,它彙總了每種食品的訂單總量。計算電子商務網站的所有訂單商品

「位置」屬性有6個可能的放置點。我有3次送貨下車時間。我想爲每個送貨時間/地點統計所有單個食品。我不知道如何使用django來做到這一點。

什麼是最好的方法來做到這一點?

我從「orderitems」模型中分離出實際的「訂單」模型。

class Order(models.Model): 
    # each individual status 
    SUBMITTED = 1 
    PROCESSED = 2 
    SHIPPED = 3 
    CANCELLED = 4 

    # set of possible order statuses 
    ORDER_STATUSES = ((SUBMITTED,'Submitted'), (PROCESSED,'Processed'), 
         (SHIPPED,'Shipped'), (CANCELLED,'Cancelled'),) 

    # order info 
    date = models.DateTimeField(auto_now_add=True) 
    status = models.IntegerField(choices=ORDER_STATUSES, default=SUBMITTED) 
    last_updated = models.DateTimeField(auto_now=True) 
    transaction_id = models.CharField(max_length=20) 


    #user info 
    user = models.ForeignKey(User, null=True) 
    full_name = models.CharField(max_length=20, blank=True) 

    # contact info 
    email = models.EmailField(max_length=50) 
    phone = models.CharField(max_length=20) 

    # Drop point info 
    location = models.CharField(max_length=50) 

    #time of the drop info 
    time = models.IntegerField() 

    # billing information 
    billing_name = models.CharField(max_length=50) 
    billing_address_1 = models.CharField(max_length=50) 
    billing_address_2 = models.CharField(max_length=50, blank=True) 
    billing_city = models.CharField(max_length=50) 
    billing_state = models.CharField(max_length=2) 
    billing_country = models.CharField(max_length=50) 
    billing_zip = models.CharField(max_length=10) 

    def __unicode__(self): 
     return 'Order #' + str(self.id) 

OrderItem的:

class OrderItem(models.Model): 
    product = models.ForeignKey(Food) 
    quantity = models.IntegerField(default=1) 
    price = models.DecimalField(max_digits=7,decimal_places=2) 
    order = models.ForeignKey(Order) 

回答

0

嘗試annotate and Count

orders = orders.annotate(num_items=Count('orderitem_set')) 

for order in orders: 
    print order.pk, order.num_items