I have used prefetch and select related blindly in real time project (Not knowing in-depth) and query time was reduced. But this explanation is too good and I got to know the real use. Thank you so much man.
Great tutorial! but can you let me know, why we are configuring django to server static files explicitly, when its already being done by runserver implicitly?
Instead of using the 'ratings' related_name and then filtering the queryset, could you have created another Prefetch object for that like you did with monthly sales and call it five_star_ratings or something?
Is the management/command pretty much the same as the scripts in the extensions just best practice to put code to import data as a command rather than a script? Also, I think there may be an error with the Prefetch() code because if I change "month_ago" to "two_days_ago": two_days_ago = timezone.now() - timezone.timedelta(days=2) and print: Sale.objects.filter(datetime__gte=two_days_ago) I get a queryset with 2 objects but when I print the below I get a queryset of 6 Restaurant.objects.prefetch_related('ratings', two_days_ago).filter(ratings__rating=5) and when I print the totals it's exactly the same as when we get all of the Sales rather than the Sales for a specific date. It's a list with 4 results. Hope this makes sense and not an issue of how I coded along.
Hey Peter - are you building a Prefetch() object in this query? It looks like you're passing in a datetime object - "two_days_ago" - to the "prefetch_related" query above?
same problem get , whenever i put , filter(ratings__rating=5, sales__date_time__range=[month_ago_30, timezone.now()- timezone.timedelta(days=3)]) then its work
@@bugbytes3923 yeah but where does the sidebar show ? my api doesnt have html page, or do i have to add atleast a form or html page ?, i use postman to test my api
@bugbytes3923 honestly it's made things kinda worse. It's sped up my landing page etc, but it's forcing me to redesign my models and come to terms with how little I understand databases.
hello sir, thanks for the explanation, i just wanted to know when will the select_related not working, I'm using a different ways to implement it here: ` loans = self.lending_loans.select_related('lender').filter(lender_loan_status__in=[Loan.STATUS.funded, Loan.STATUS.completed]) ` Where lending_loans is the related name of the the FK inside the loan model.
Great video! restaurants = Restaurant.objects.prefetch_related('rating_set', monthly_sales).filter(rating__rating=5) restaurants.annotate(total=Sum('sale__income')) I'm cross referencing the values printed out on the terminal vs the dB Browser table. The Sum value doubles up for Restaurant rows when the query set returns the same restaurant name twice (i.e. Bombay Bustle has 2-off 5-star entries, and it's sales are summed x2 - instead of the x1 which is expected). I tried troubleshooting with ChatGPT using the distinct() function on the filtered query set. It worked on the query set, but did not translate with the annotate function. Just wondering if anyone else encountered the same anomaly?
Playlist link: th-cam.com/video/EsBqIZmR2Uc/w-d-xo.html
You are the best thing that has ever happened to Django. Thank you. The best thing we can do, is buy you coffee. You deserve it.
Haha bold claim 😃 But thank you, that's amazing to hear!
I've watched so many Django videos and you're the first person I've come across that talks about this. Great work once again!
Thanks a lot Peter!
Thanks, You have taken Django to another level❤
love your cool advanced content... you are my django master. thank you for sharing ❤
Thanks a lot, really appreciate that!
excellent, thanks again! been watching your videos for a while now.. very well explained!
Thanks a lot - great to hear that! Thank you for watching
Great content, easy concise and well paced. You are a real masterclass educator.
Thanks a lot, really appreciate that comment ! Glad you liked the video.
Thanks, You have taken me to another level in django
Thanks a lot, glad to hear that!
Your channel is peace of gold on youtube. Exact hit that I'd been searching for
Thanks so much for that comment, really appreciate it!
You are true genius❤
Thanks a lot!
Thanks alot, was wondering why my application was so slow and this completely fixed it. Love this channel!
Glad to hear it! Thanks a lot!
I have used prefetch and select related blindly in real time project (Not knowing in-depth) and query time was reduced. But this explanation is too good and I got to know the real use. Thank you so much man.
Thanks a lot! Really glad to hear it was helpful!
Nice video! Very helpful.
Thank you!
Dude i love ur stuff. For me the special highlight tho is every time you say boolean I hear boullion
Haha thanks, some words do come across strangely in a Scottish accent! Thanks for watching!
Awesome as always 🥳🎉
Thanks a lot 🙏
Thank you so much for this great channel 🥰🥰
Thanks
Thanks so much for this, much appreciated!
awesome
Thanks a lot!
Love the content
Thanks a lot!
Very important video.
Cheers!
Excellent video!! Thank you!
Thanks a lot!
Thank you!
You're welcome, thanks for watching Tomislav!
Amazing
Thank you!
thanks
Thanks for watching!
Nice, Thanks!
You’re welcome and thanks for watching!
Great!
Thanks a lot!
Thank you
You're welcome, thanks for watching!
I hope your channel be 1m subs ❤
Thanks a lot!!
Sure it is very good
Thanks!
Thank you, sir
Awesome!
Great tutorial! but can you let me know,
why we are configuring django to server static files explicitly,
when its already being done by runserver implicitly?
thank
Thanks!
awesome video❤... could you also post a video on caching in django
Thanks - yes I’ll be doing some Caching + Redis stuff soon!
Instead of using the 'ratings' related_name and then filtering the queryset, could you have created another Prefetch object for that like you did with monthly sales and call it five_star_ratings or something?
Is it mandatory for me to work on templates for each endpoints if i wish to use the django debug toolbar?
I have updated date of sale of a restaurant to two months before. But still I'm getting the sale object
Is debugtoolbar still usable for rest APIs?
nice video ! Do you have any suggestions for debugging the queries with django rest framework ?
What about ur Django-leaflet series?
I’ll hopefully create some more soon!
Is the management/command pretty much the same as the scripts in the extensions just best practice to put code to import data as a command rather than a script?
Also, I think there may be an error with the Prefetch() code because if I change "month_ago" to "two_days_ago":
two_days_ago = timezone.now() - timezone.timedelta(days=2)
and print:
Sale.objects.filter(datetime__gte=two_days_ago)
I get a queryset with 2 objects but when I print the below I get a queryset of 6
Restaurant.objects.prefetch_related('ratings', two_days_ago).filter(ratings__rating=5)
and when I print the totals it's exactly the same as when we get all of the Sales rather than the Sales for a specific date.
It's a list with 4 results.
Hope this makes sense and not an issue of how I coded along.
Hey Peter - are you building a Prefetch() object in this query? It looks like you're passing in a datetime object - "two_days_ago" - to the "prefetch_related" query above?
same problem get , whenever i put , filter(ratings__rating=5, sales__date_time__range=[month_ago_30, timezone.now()- timezone.timedelta(days=3)]) then its work
what if im using django rest framework and im working on api ?
@@bengabp on your api you’ll usually be fetching data from the database, so all the same concepts will apply
@@bugbytes3923 yeah but where does the sidebar show ? my api doesnt have html page, or do i have to add atleast a form or html page ?, i use postman to test my api
N+1 has been kicking my @ss
Hope this can help 😁
@bugbytes3923 honestly it's made things kinda worse. It's sped up my landing page etc, but it's forcing me to redesign my models and come to terms with how little I understand databases.
hello sir, thanks for the explanation, i just wanted to know when will the select_related not working,
I'm using a different ways to implement it here:
` loans = self.lending_loans.select_related('lender').filter(lender_loan_status__in=[Loan.STATUS.funded, Loan.STATUS.completed])
`
Where
lending_loans is the related name of the the FK inside the loan model.
Great video!
restaurants = Restaurant.objects.prefetch_related('rating_set', monthly_sales).filter(rating__rating=5)
restaurants.annotate(total=Sum('sale__income'))
I'm cross referencing the values printed out on the terminal vs the dB Browser table. The Sum value doubles up for Restaurant rows when the query set returns the same restaurant name twice (i.e. Bombay Bustle has 2-off 5-star entries, and it's sales are summed x2 - instead of the x1 which is expected).
I tried troubleshooting with ChatGPT using the distinct() function on the filtered query set. It worked on the query set, but did not translate with the annotate function.
Just wondering if anyone else encountered the same anomaly?