Django今天添加的对象数量

时间:2014-07-15 20:34:22

标签: python django for-loop

我有一个像这样的models.py:

class Baslik(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    title = models.CharField(max_length=50)
    slug = models.SlugField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return str(self.title)


    def get_absolute_url(self, ):
        return reverse('baslik', args=[self.slug])

    class Meta:
         ordering = ['updated']



class Entry(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    baslik = models.ForeignKey(Baslik, null=True, blank=True)
    icerik = models.TextField(max_length=50000)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return str(self.icerik)

我需要显示每个baslik当前日期中添加的条目数,这是其中的ForeignKey。我使用这个views.py:

def hepsi(request):
    basliklar = Baslik.objects.filter(active=True).order_by('-updated')  
    rast = Entry.objects.order_by('?')[0]
    gunlist = []
    zaman1 = date.today()
    zaman2 = str(zaman1).split(" ")
    zamangun = zaman2[0]

    for i in basliklar:
        entryler = i.entry_set.all()
        sayi2 = entryler.count()

        if sayi2>0:
            ent1 = entryler.latest('id')
            ent2 = ent1.updated
            ent3 = str(ent2).split(" ")
            entgun = ent3[0]

            if entgun == zamangun:
                gunlist.append(i)

        if sayi2==0:
            baslik1 = i.updated
            baslik2 = str(baslik1).split(" ")
            baslikgun = baslik2[0]

            if baslikgun==zamangun:
                gunlist.append(i)

    cta = {'basliklar': basliklar, 'entryler': entryler, 'ent1': ent1, 'ent2': ent2, 'entgun': entgun, 'zamangun': zamangun, 'gunlist': gunlist, 'rast': rast}

    return render(request, "base.html", cta)

我需要今天在base.html的这部分for循环中添加的条目数:

{%for my in gunlist%}

 {{ i }} {{ # of entries added today }}

{%endfor%}

我该如何解决这个问题?任何意见都会有帮助。感谢。

2 个答案:

答案 0 :(得分:0)

这适用于views.py:

all_basliks=Baslik.objects.filter(active=True).order_by('-updated')

for baslik in all_basliks:
 number=baslik.Entry_set.filter(timestamp=localtime.date()).count()
 gunlist[baslik]=number

return render(request, 'gunlist': gunlist)

在base.html中:

{% for i in gunlist %}
 {{ i }} {{ gunlist_i }}
{% endfor %}

答案 1 :(得分:0)

这个版本的带有OrderedDict的hepsi功能正常工作。

def hepsi(request):
    basliklar = Baslik.objects.filter(active=True).order_by('-updated') 
    rast = Entry.objects.order_by('?')[0]
    gunlist = OrderedDict([])
    zaman1 = date.today()
    zaman2 = str(zaman1).split(" ")
    zamangun = zaman2[0]

    for i in basliklar:
        entryler = i.entry_set.all()
        sayi2 = entryler.count()

        if sayi2>0:
            j=0
            for enty in entryler:
                entyy1 = enty.updated
                entyy2 = str(entyy1).split(" ")
                entyygun = entyy2[0]
                if entyygun == zamangun:
                    j += 1
            gunlist.update({i:j})

        if sayi2==0:
            baslik1 = i.updated
            baslik2 = str(baslik1).split(" ")
            baslikgun = baslik2[0]

            if baslikgun==zamangun:
                gunlist.update({i:'0'})

    cta = {'basliklar': basliklar, 'entryler': entryler, 'entyygun': entyygun, 'zamangun': zamangun, 'gunlist': gunlist.iteritems(), 'rast': rast}

    return render(request, "base.html", cta)