Django对文章的看法

时间:2018-03-27 17:41:36

标签: python django django-models django-views

如何计算文章的唯一视图数量?

class Article(models.Model):
    title = models.CharField(max_length = 300)
    post = RichTextUploadingField(blank=True, default='')
    date = models.DateTimeField(auto_now=True)
    views = models.IntegerField(default='0')



    def __str__(self):
        return "%s" % (self.title)

1 个答案:

答案 0 :(得分:0)

您可能希望将唯一匹配存储在如下模型中:

from django.contrib.auth.models import User


class HitCount(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    hits = models.PositiveIntegerField()

然后要获得统计数据,你会做类似的事情:

all_hits_by_specific_user = Hitcount.objects.filter(article=somearticle,
                                                    user=someuser).first().hits

total_unique_hits = HitCount.objects.count()