Garrett
Garrett

Reputation: 1792

Django: Trying to create a user score system

So instead of making this into a new app or actually add it to the User model I want to just calculate this in the view. So here are two lines of code that I've added to get the number of posts a user has created and then multiplying it by 10 (the value I'm giving to adding a post) in the view and then passing it into the template. However, I am getting an error.

Not sure if I'm going about this in the best way, but heres what I have.

Code the in the view:

posts = UserPost.objects.filter(author=user)
posts_count = posts.len() * 10

The error:

AttributeError at /user/2/
'QuerySet' object has no attribute 'len'

I also tried with .count instead of .len()

Upvotes: 0

Views: 344

Answers (1)

Serg Anuke
Serg Anuke

Reputation: 168

Try this:

posts = UserPost.objects.filter(author=user)
posts.count()

Upvotes: 2

Related Questions