这就是我想要做的事情:我有Post模型,人们可以对该帖子进行投票。并且使用我的models.py中的函数完成该投票的计算。我希望能够在models.py中控制该投票。希望通过代码,问题更清晰。
class Post(models.Model):
title = models.CharField(max_length = 100)
moderator = models.ForeignKey(User)
views = models.IntegerField(default=0)
slug = models.CharField(max_length=255, unique=True)
content = RichTextUploadingField(config_name='default')
get_vote_count = models.IntegerField(default=1)
def get_vote_count(self):
vote_count = self.vote_set.filter(is_up=True).count() - self.vote_set.filter(is_up=False).count() +1
if vote_count >= 0:
return "+ " + str(vote_count)
else:
return "- " + str(abs(vote_count))
在admin.py
中class PostAdmin(admin.ModelAdmin):
fields = ['category','url','video','title',
'content','image','get_vote_count']
readonly_fields = ('get_vote_count',)
class Meta:
model = Post
然后get_vote_count显示投票但我无法控制它。有人可以教我怎么样?