2013-12-23 57 views
0

我有一個新聞源的動作模型。像這樣:如何在同一日期顯示創建的操作?

class Action(models.Model): 
    actor = models.ForeignKey(User) 
    target_object_id = models.PositiveIntegerField() 
    target_content_type= models.ForeignKey(
       ContentType, 
       related_name='target' 
     ) 
    target = generic.GenericForeignKey(
       'target_content_type', 
       'target_object_id' 
     ) 
    verb = models.CharField(max_length=200) 
    date = models.DateTimeField(default=datetime.now()) 

    def __unicode__(self): 
       return "{0} {1}".format(
         self.actor, 
         self.verb, 

         ) 

我想通過今天創建一個來顯示一個行動。布我想要顯示這樣的舊操作:

22.12.2013 - 2 posts created for A Blog 
22.12.2013 - x followed 3 people 
21.12.2013 - 3 posts created for B Blog 
21.12.2013 - 4 channel created 

這是用戶的新聞源。我怎麼能根據日期做到這一點?

+0

你能發表您的看法? – niekas

回答

0

您可以在您的視圖模板準備背景下,這樣的:

from datetime import datetime, timedelta 
from django.shortcuts import render_to_response 

def my_view(request): 
    yesterday = datetime.today().date() - timedelta(1) 
    return render_to_response('my_template.html', { 
     'new_actions': Action.objects.filter(date__gt=yesterday), 
     'old_actions_count': len(Action.objects.filter(date__lt=yesterday)), 
    }) 

然後你就可以在你的模板中使用上下文:

用於操作模式
{% for action in new_actions %} 
    {{ action }} 
{% endfor %} 
There was {{ old_actions_count }} older actions. 
+0

沒有這個不行。動作模型有不同的動作(跟隨,創建,評論vs vs) –