Django select_related Examples |Django select_related and prefetch_related |Django n+1 Query Problem

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ส.ค. 2024

ความคิดเห็น • 104

  • @CodeBand
    @CodeBand  4 ปีที่แล้ว +2

    Django prefetch_related | Django select_related vs prefetch_related : th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

  • @evanwhite4190
    @evanwhite4190 3 ปีที่แล้ว +12

    This makes so much more sense than the documentation. Thank you!

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Glad to hear that...you can also checkout prefetch_related here :
      th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

  • @sayadahmedshaurov8639
    @sayadahmedshaurov8639 3 ปีที่แล้ว +4

    I was looking for this kind of explanation with example. What everyone does is just talks about theory. Thanks for making more sense by giving an example.

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +2

      Glad to hear that brother...😊

  • @rahul_singh_rajput3292
    @rahul_singh_rajput3292 ปีที่แล้ว +1

    hhmm. delicious example 😌🔥..

    • @CodeBand
      @CodeBand  ปีที่แล้ว +1

      Thank you 🙌

  • @likeitthings8418
    @likeitthings8418 2 ปีที่แล้ว +1

    thank you so much it helped me alot

    • @CodeBand
      @CodeBand  ปีที่แล้ว +1

      Glad it helped!

  • @oruchkin
    @oruchkin 2 ปีที่แล้ว +1

    Very good thanks

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Most welcome

  • @PrriyanshuSharmaa
    @PrriyanshuSharmaa 3 ปีที่แล้ว +1

    Thank You so much bro.

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +2

      Glad to hear that😊

  • @vinodinti
    @vinodinti 3 ปีที่แล้ว +1

    very good bro.

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Thanks a lot👍🏻

  • @amirhosseinebneroomi7280
    @amirhosseinebneroomi7280 2 ปีที่แล้ว +1

    amazing..

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +2

      Thanks a lot

  • @mohammadfaisal2039
    @mohammadfaisal2039 ปีที่แล้ว +1

    Thank you, for good explanations

    • @CodeBand
      @CodeBand  ปีที่แล้ว +2

      Glad to hear that :)

  • @zoromahm
    @zoromahm 4 ปีที่แล้ว +1

    So useful. Thanks for the video.

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Glad it was helpful!

  • @mmafhbmmafhb3555
    @mmafhbmmafhb3555 3 ปีที่แล้ว +1

    thank you , you save me :)

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      You're welcome!

  • @hachhh
    @hachhh 11 หลายเดือนก่อน

    Thank you!! Very clear explanation.

  • @user-ib4pu5zr7l
    @user-ib4pu5zr7l 3 ปีที่แล้ว +1

    Thank you ! The explanation is awesome)

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Glad to hear that...😊

  • @behruzisaev2102
    @behruzisaev2102 2 ปีที่แล้ว +1

    Great explanation, bro. Thanks a lot!

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +2

      Glad it was helpful!

  • @nikeshthakor406
    @nikeshthakor406 2 ปีที่แล้ว +1

    Thanks

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Welcome.. :)

  • @omidasadi2264
    @omidasadi2264 2 ปีที่แล้ว +1

    it was great, thanks for sharing

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Glad you enjoyed it

  • @santiagobustamante970
    @santiagobustamante970 4 ปีที่แล้ว +1

    Great video and thanks for the tool, so useful! you saved hours of my life
    Thanks again man

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      Glad it helped! Keep supporting 😊

  • @ricardomoha14
    @ricardomoha14 2 ปีที่แล้ว +1

    Your video is really great, up to the point and easy to understand, just onething.. Try being a bit faster.. I think the video could have been done in 5 minutes instead of 12.

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Thanks a lot and I have noted down the suggestion and will try to improve in future...

  • @MuhammadMahirQ8
    @MuhammadMahirQ8 4 ปีที่แล้ว +1

    awesome broo.. highly appreciated

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      Glad to hear that brother...Please share in your circle too...Thanks a lot... :)

  • @romanbush5164
    @romanbush5164 11 หลายเดือนก่อน

    ThankS!

  • @SAIFHASANAT-cm8xh
    @SAIFHASANAT-cm8xh 3 หลายเดือนก่อน

    thank you

  • @azizbekgulomov2881
    @azizbekgulomov2881 3 ปีที่แล้ว +1

    Yeah good job! Keep it up!

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Thanks a lot👍🏻

  • @digambar6191
    @digambar6191 ปีที่แล้ว

    Thanx sir

  • @harshamachine_learning7831
    @harshamachine_learning7831 4 ปีที่แล้ว +1

    Thanks for select related
    Please include prefetch related asap

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Definitely, I will upload it very soon. Thanks😊

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      I have done the video of prefetch_related.😊 Please do let me know your suggestions in the comments. Thank you☺️
      th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

    • @harshamachine_learning7831
      @harshamachine_learning7831 4 ปีที่แล้ว +1

      @@CodeBand video looks good, it covers key basic areas of that method

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Thanks❤️

  • @josemanuel9137
    @josemanuel9137 4 ปีที่แล้ว +1

    good explanation bro!

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Glad you liked it! Thanks for the support and keep supporting :)

  • @imranshaikh-bt5ln
    @imranshaikh-bt5ln 6 หลายเดือนก่อน

    Hi which extension have you added on your browser to show requests and response time?

  • @joshuahadap2373
    @joshuahadap2373 4 ปีที่แล้ว +1

    All I can say is I love you!

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Thanks a lot...Keep supporting... ☺️

  • @bricegnanago409
    @bricegnanago409 3 ปีที่แล้ว +1

    So good ! Now how make a left join query with select_related please

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Glad to hear that...have a quick research on left join and you'll find an answer...keep going👍🏻

  • @rangabharath4253
    @rangabharath4253 4 ปีที่แล้ว +1

    awesome

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Thanks a lot 😊

  • @pritamghosh270
    @pritamghosh270 4 ปีที่แล้ว +1

    Great explained. Thanks 😊 . If I want objects of Employee table from Department then what to do?
    For an expample, I want some employees under a specific department. But I want it from the Department model not from Employee. That time what to do?

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      Glad to hear that😊. In the case you mentioned, there is no chance of raising an N+1 query problem, because, department id will be there with you, and you just need to fetch all the employees with that specific dept. id, so at most 2 queries will be required. Hope this helps and I recommend you to watch the prefetch_related too...
      th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

  • @danarardiwinanto
    @danarardiwinanto 2 ปีที่แล้ว +1

    Sir... I want to output count employee per department. let's say I have 8 departments, and I want to outputing all department with and without employee. How can I do this? Thankyou

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +2

      Sorry for the late reply...
      Did you figured it out?

    • @danarardiwinanto
      @danarardiwinanto 2 ปีที่แล้ว +1

      @@CodeBand Yes, I have figured it out. I need to annotate (right join) my employee_id on department table. Should be like this: Department.objects.values('id_department').annotate(Counter=Count('employee')).values('id_department', 'Counter')

  • @misfarsiddeek3105
    @misfarsiddeek3105 4 ปีที่แล้ว +2

    Brother, can you do a complete CRUD video with validation also? For the data input please use raw html forms. Without using Model Form or django form.

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      Will try to do that in the future brother...Thanks for the suggestion... :)

    • @misfarsiddeek3105
      @misfarsiddeek3105 4 ปีที่แล้ว +1

      @@CodeBand sure. I am waiting for that video.

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      :)

  • @RedElectrik
    @RedElectrik 4 ปีที่แล้ว +6

    You should remove "prefetch_related" from the title. You don't talk about it

    • @RedElectrik
      @RedElectrik 4 ปีที่แล้ว +2

      Your also dont seem to have made a video about it anywhere

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +3

      Actually, I planned to do both in a single video, but unfortunately, I couldn't. I will definitely make a video on prefetch_related very soon, I planned it earlier, but somehow I got into some other videos. Sorry for the inconvenience caused.😔

    • @RedElectrik
      @RedElectrik 4 ปีที่แล้ว +1

      Probs still a good idea to remove that out the title

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      I have done the video of prefetch_related.😊 I also discuss when to use prefetch_related or select_related and their differences in the video. Please do let me know your suggestions in the comments. Thank you☺️
      th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

    • @RedElectrik
      @RedElectrik 4 ปีที่แล้ว +1

      @@CodeBand I mean the title for this is still prefetch_related. Seems somewhat misleading

  • @deepanshuaggarwal7042
    @deepanshuaggarwal7042 3 ปีที่แล้ว +1

    we can use select_related only in ForeignKey case? or we can use with other joins also

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +2

      select_related is limited to single-valued relationships - foreign key and one-to-one.
      For more clarification, please see this post in SO.
      stackoverflow.com/questions/31237042/whats-the-difference-between-select-related-and-prefetch-related-in-django-orm

  • @pankaj9575
    @pankaj9575 3 ปีที่แล้ว +1

    Getting error while migrations, Cannot add or update a child row,a foreign key constraint fails

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Please post your models.py so that I could help...😊

  • @yollaifliandry495
    @yollaifliandry495 2 ปีที่แล้ว +1

    Thank you. By the way, is it possible to use select_related in class-based view ?

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      Hey, for sure. It doesn't matter whether you use CBV/FBV.

  • @FitriWibowo
    @FitriWibowo 4 ปีที่แล้ว +1

    Very nice video, can we do the same thing with the Q keyword in Django

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Thanks a lot brother...There are a couple of ways of doing it...and this one stands out in terms of performance... :)

  • @jmdesros
    @jmdesros 2 ปีที่แล้ว +1

    What if an employee doesn't have a department?

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +2

      Sorry for the late reply...
      That depends on the foreign key configurations.

  • @mfsdrawings8600
    @mfsdrawings8600 4 ปีที่แล้ว +2

    👍🏻👍🏻👍🏻👍🏻❤️

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Thanks a lot brother... :)

  • @misfarsiddeek3105
    @misfarsiddeek3105 4 ปีที่แล้ว +1

    Hi. Great brother.
    May know that what is the different between mentioning Foreignkey, many to many and one to many in our model? Are they same?

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      Thanks a lot brother...and about mentioning the foreign key and all, it totally depends on what kind of relationship you wanna use... I recommend you to watch this playlist...you will get a better idea...
      th-cam.com/play/PLbasZIkCgHJH38p2WkC5GTsgoV_8hlQ4A.html
      And also please refer this article...
      hackernoon.com/django-orm-relationships-cheat-sheet-14433d6cf68c

    • @misfarsiddeek3105
      @misfarsiddeek3105 4 ปีที่แล้ว +1

      @@CodeBand ok brother. I will refer these and ask from u if I have doubts. Thanx for helping me to learn django.

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      @@misfarsiddeek3105 It's my pleasure brother...I will definitely help you out in case...Thanks :)

    • @misfarsiddeek3105
      @misfarsiddeek3105 4 ปีที่แล้ว +1

      @@CodeBand thnx brother. ❤️

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      @@misfarsiddeek3105 Welcome brother..:)

  • @rojanshrestha822
    @rojanshrestha822 4 ปีที่แล้ว +1

    Thanks!! By the way, where is the video on prefetch_related??

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      Glad it was helpful.
      I will actually do the prefetch_related in a future video... :)
      Thank you :)

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      I have done the video of prefetch_related.😊 I also discuss when to use prefetch_related or select_related and their differences in the video. Please do let me know your suggestions in the comments. Thank you☺️
      th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

  • @abhisharma8526
    @abhisharma8526 4 ปีที่แล้ว +1

    hello sir ...please make a video of prefetch_related today...it's urgent for me ....please

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +2

      I'm very sorry...I have not scheduled any video for today...but I can assure, the next video is prefetch_related. Meanwhile, you can refer other resources. Thank you for the support😊

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +3

      I have done the video of prefetch_related.😊
      th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

  • @paranoia9142
    @paranoia9142 2 ปีที่แล้ว +1

    Will this work on 3 models?

    • @CodeBand
      @CodeBand  2 ปีที่แล้ว +1

      You can use __(double underscores) to access related field, see this : stackoverflow.com/questions/19909672/django-prefetch-related-on-multiple-models

  • @RedElectrik
    @RedElectrik 4 ปีที่แล้ว +1

    You gonna remove "prefetch_related" for the title yet?

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      I'm planning to do the next video in prefetch_related, so that I could put up both of them as a playlist and hence it would not be a big issue. Please let me know your view on this😊

    • @CodeBand
      @CodeBand  4 ปีที่แล้ว +1

      I have done the video of prefetch_related.😊 I also discuss when to use prefetch_related or select_related and their differences in the video. Please do let me know your suggestions in the comments. Thank you☺️
      th-cam.com/video/5-UN4YPDDQc/w-d-xo.html

  • @flowing_dance
    @flowing_dance 3 ปีที่แล้ว +1

    why people use fake accent ..its so irritating!

    • @CodeBand
      @CodeBand  3 ปีที่แล้ว +1

      Sorry to hear that...Honestly, it's not made up...its original😊