I have a project that may give points to persons who post.
I have two models (Post and Points).
I want to count each user post in (Post) and multiply the result to create_post_points field in (Points) and show the result in template.
What i tried to do (signals, aggregation, queryset, intermediate class in models) but i failed.
Can any one help me?
Models.py
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
likes = models.ManyToManyField(User, related_name='likes', blank=True)
post_date = models.DateTimeField(default=timezone.now)
post_update = models.DateTimeField(auto_now=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
favourite = models.ManyToManyField(User, related_name='favourite', blank=True)
class Points (models.Model):
create_post_points = models.IntegerField()
create_comment_points = models.IntegerField()
Views.py
def PostListView(request):
posts = Post.objects.all()
context = {
'title': 'Home Page',
'posts': posts,
}
return render(request, 'blog/index.html', context)
Templates
{% for post in posts %}
<div class="border p-3 mb-3">
<div class="media">
<img class="img-fluid rounded-circle border m-2" style="width: 70px; height:70px;"
src="{{post.author.profile.image.url}}" alt="User Photo">
<div class="media-body">
<h5><a class="text-primary" href="{% url 'detail' post.id %}">{{post.title}}</a></h5>
<h6 class="text-dark">Auther Score : {{******}}</h6>
<p>{{post.content |truncatechars:150 }}
<a class="text-primary" href="{% url 'detail' post.id %}">read more</a></p>
</div>
</div>
</div>
{% endfor %}