Count elements in Django

Viewed 40

I am learning Django. I have a question about counting elements from tables.

class Opinion(models.Model):
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
    users = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
    books = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    rate = models.IntegerField()
    description = models.TextField()

    def __str__(self):
        return self.user_id.username + ' ' + self.book_id.title 

My table Opinion is in relationship with built-in table User. I am wondering how can I count how many opinions each user has added?

I would like to show information about users and the amount of opinions they have added on my website.

1 Answers
Related