WebMay 18, 2024 · 2 Answers. The Prefetch is being executed as a 2nd query after the products query, thus its not possible to filter out products based on the prefetch. You need to repeat the prefetch filtration as either Subquery or inside the Count which you are trying to make. filtered_qs = Product.objects.annotate ( offers_count=Count ('offers', filter=Q ... WebJun 27, 2024 · You can read about it from here Django1.1 Prefetch Objects and this is an example of what you're looking for from django.db.models import Prefetch newest = Comment.objects.filter (do_your_filters_here) Post.objects.prefetch_related (Prefetch ("comments", queryset=newest, to_attr="newest_comments")) Share Follow answered …
Filtering a django Prefetch based on more fields from the root …
WebMay 17, 2024 · 1. Django fetches related tables at runtime: each call to productSpy.product will fetch from the table product using productSpy.id. The latency in I/O operation means that this code is highly inefficient. using prefetch_related will fetch product for all the product spy objects in one shot resulting in better performance. # Get the amount of ... WebApr 29, 2024 · The answer to this question suggests I could use a subquery here but I don't understand how it could ever work: you have to put the subquery inside a query to work in the Prefetch, and the OuterRef refers to that object/DB row; there is no way to get back to the original, root object. st michaels magheralin gac
Filtering Prefetch object using OuterRef and SubQuery : …
WebJul 2, 2024 · I have tried to approach this problem with select_related, prefetch_related, annotate, and subquery, but I havn't been able to get the results I need. Ideally, each Corporation in the queryset should be annotated with an integer type_10_count which reflects the number of departments of that type. WebFeb 26, 2024 · ArrayAgg will be great if you want to fetch only one variable (ie. name) from all articles. If you need more, there is a better option for that: prefetch_related. Instead, you can prefetch for each Order, latest OrderOperation as a whole object. This adds the ability to easily get any field from OrderOperation without extra magic.. The only caveat with … WebBUT In pure Django, ignoring REST API, you should be able to do this with something like: Performance.objects.all ().prefetch_related ( Prefetch ('staff', queryset=Staff.objects.all ().prefetch_related ( Prefetch ('service', queryset=Service.objects.all ().prefetch_related ( Prefetch ('location__services') ) ) ) Couple pointers: st michaels manor butleigh