I have a auth_user
model, and a posts
model. the posts
model has fields like: id
, author_id
and likes
. All I need print likes
values related to each user
in the base
template. like:
{{ user.posts.likes }}
or
{{ user.user_id.posts.likes }}
This is my posts model:
class Post(models.Model):
title = models.CharField(max_length=150)
author = models.ForeignKey(User, on_delete=models.CASCADE)
likes = models.PositiveIntegerField(default=0, blank=True)
class Meta:
db_table = "posts"
def total_likes(self):
return self.likes
however it does not work, while {{ user.profile.image.url }}
(profile is another model which has user_id
) works perfectly.
I am importing base
templates to other templates like {% extends "app/base.html" %}
so I don't see any place in backend to pass the likes
values