2015-01-20 83 views
0

說我有一個模型是這樣的:Django的 - 時間字段更改爲日期字段

class Arrival(models.Model): 
    client = models.ForeignKey(User, related_name='client') 
    time = models.DateTimeField() 

現在我想用繪製在每個日期的唯一客戶端的訪問量曲線圖。

我目前的做法是使用:

Arrival.objects.values('client','time') 

之後,將所有的日期時間字段轉換爲日期字段。使用python庫獲取唯一日期列表,然後遍歷客戶端以查明每個日期有多少客戶端訪問。

請問有沒有人有更高效的方法?

回答

1

轉貼我的答案在這裏:

Get daily counts of objects from Django

(Arrival.objects 
    # get specific dates (not hours for example) and store in "created" 
    .extra({'created':"date(time)"}) 
    # get a values list of only "created" defined earlier 
    .values('created') 
    # annotate each day by Count of Arrival objects 
    .annotate(created_count=Count('id'))) 
+0

嘿,非常感謝評論。但我認爲這件事不會過濾掉重複訪問。你會推薦什麼方法來只計算那些獨特的ID? – user1819047 2015-01-21 01:18:39

+0

你怎麼定義唯一性。您希望每天統計訪問次數,但每個客戶需要統計一次? – 2015-01-21 18:20:00