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)

1 Answers1

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()
Druhin Bala
  • 772
  • 7
  • 12