Answer the question
In order to leave comments, you need to log in
How to get one last object in each category?
models.py
class Post(models.Model):
title = ...
text = ...
category = models.ForeignKey('CategoryPost', ...)
class CategoryPost(models.Model)
title = ...
slug = ...
last_posts = []
for category in CategoryPost.objects.all():
post = Post.objects.filter(category=category).last()
last_posts.append(post)
Answer the question
In order to leave comments, you need to log in
And if you try MyModel.objects.all().count(), the resulting number can be used to access through pk.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question