1
在Django的一個簡單的論壇應用程序中,我想在每個線程上呈現幾個用戶在同一頁面上的帖子(在用戶帖子的用戶面板上,就像所有傳統論壇)。如何計算用戶在Django的帖子?
下面是型號:
class Post(models.Model):
title = models.CharField(max_length=75, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
creator = models.ForeignKey(User, blank=True, null=True)
updated = models.DateTimeField(auto_now=True)
topic = models.ForeignKey(Topic)
body = models.TextField(max_length=10000)
class Topic(models.Model):
title = models.CharField(max_length=100)
description = models.TextField(max_length=10000, null=True)
forum = models.ForeignKey(Forum)
created = models.DateTimeField()
creator = models.ForeignKey(User, blank=True, null=True)
updated = models.DateTimeField(auto_now=True)
closed = models.BooleanField(blank=True, default=False)
published = models.BooleanField(blank=True, default=False)
visits = models.IntegerField(default = 0)
weight = models.IntegerField(blank=True, default=0)
slug = models.CharField(max_length=100, blank=True)
def num_posts(self):
return self.post_set.count()
def num_replies(self):
return max(0, self.post_set.count() - 1)
def last_post(self):
if self.post_set.count():
return self.post_set.order_by("-created")[0]
def __unicode__(self):
return unicode(self.creator) + " - " + self.title
def save(self, *args, **kwargs):
super(Topic, self).save(*args, **kwargs)
我也有這種奇怪的模式:
class PostCount(models.Model):
user = models.OneToOneField(User)
posts = models.IntegerField(default=0)
@classmethod
def create(cls, user):
postcount = cls(user=user)
return postcount
不知何故神奇地由用戶(而不是職位數)返回主題的號碼,這樣他們可以可以使用{{topic.creator.postcount.posts}}
在模板中進行訪問。
和使議題的觀點:
def topic(request, topic_id):
"""Listing of posts in a topic."""
posts = Post.objects.filter(topic=topic_id).order_by("created")
posts = mk_paginator(request, posts, DJANGO_SIMPLE_FORUM_REPLIES_PER_PAGE)
topic = Topic.objects.get(pk=topic_id)
topic.visits += 1
topic.save()
forum = topic.forum
return render_to_response("myforum/topic.html", add_csrf(request, posts=posts, pk=topic_id,
topic=topic, forum= forum), context_instance=RequestContext(request))
所以我不知道如何最好的模板,用戶擁有的職位數?
這並不表明在任何模板。也請不要說有幾個用戶參與。這個查詢集如何捕獲它們? – Jand