[Answered ]-How to get only the most recent post from each user, ordered by created date

1👍

If you could access the usermodel in the template, you could do this:

{% for friend in friends %}
    {% for post in friend.posts_set.all|dictsort:"created_at" %} // or dictsortreversed 
        {{post.data}}
    {%endfor%}
{%endfor%}

If you have custom user models, go the extra step and define some methods on that to access the posts:

class User(models.Model):
    ....
    def get_latest_posts(self):
        return self.posts_set.all().order_by("-created_on")

And then in the template access it like:

{% for friend in friends %}
    {% for post in friend.get_latest_posts %}
        {{post.data}}
    {% endfor %}
{% endfor %}

Leave a comment