dabapps / django-readers

A lightweight function-oriented toolkit for better organisation of business logic and efficient selection and projection of data in Django projects.
https://www.django-readers.org
BSD 2-Clause "Simplified" License
183 stars 7 forks source link

Is limiting relationships possible? #58

Closed adammsteele closed 1 year ago

adammsteele commented 2 years ago

Hi,

Just trying to get my head around what is and isn't supported, the use case I'm after would be something like an Author with many Books, and writing a spec to show the author and the top 5 books by some ordering criteria.

I tried a couple of approaches to no avail:

Let me know if you would prefer questions to be asked on a different platform.

Thanks for the library.

j4mie commented 2 years ago

Hi! Thanks for trying out django-readers 😎

I think the question isn't really readers-specific: readers just uses prefetch_related to load relationships, so what you need to be able to do is prefetch a limited number of objects. This article suggests a few ways to do it, involving OuterRef and Subquery. If you can get this to work with "just" your queryset (ie without readers) then it should be fairly straightforward to convert this into a reader pair.

I'll leave this ticket open because I'm very interested to hear how you get on!