B
B
bbjob2022-01-27 10:00:26
Django
bbjob, 2022-01-27 10:00:26

How to correctly form a QuerySet query to get several nested objects from each object?

For example, there is a list of category IDs = [56, 67, 59]
How to correctly form QS so that in one query to the database to get 5 posts of each of these categories?
Now I have this:

def test(self):
        categories = [56, 67, 59]
        queryset = Post.objects.filter(
            category__in=categories).distinct()[:15]

        return queryset

But it turns out that if there are more than 15 posts in the category ID 56, then the final QS is completely filled with posts of this category

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question