【发布时间】:2010-10-29 01:59:36
【问题描述】:
该站点使用 2 个对象 - 文章和博客。每次查看文章或博客时,相关计数器应增加一。
这个想法是有一个“前十”的应用程序来衡量文章和条目的“流行度”。
因为我使用了多个对象,所以我希望 Tracker 模型对相关对象使用 genericForeignKey。
#models.py
class Tracker(models.Model):
count = models.PositiveIntegerField(default=1)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
def hit(self):
self.count += 1
我想写一个包装视图函数的装饰器,但可能没有必要。
谢谢
【问题讨论】:
标签: django django-models content-type decorator