1👍
✅
One way is
class Category(models.Model):
catID = models.CharField(max_length=20, primary_key=True)
title = models.CharField(max_length=200)
description = models.CharField(max_length=200)
def latest_post(self):
post = self.post_set.order_by('-pub_date')
if post:
return post[0]
return None
and in the template,
{% for cat in forum_cats %}
{% if cat.latest_post %}
{{cat.latest_post.title}}
{% endif %}
{% endfor %}
Source:stackexchange.com