0

How to count the number of unique views on articles?

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)
3

1 Answer 1

0

You probably want to store the unique hits in a model like this:

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()

And then to get stats you would do something like:

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

total_unique_hits = HitCount.objects.count()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.