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

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • In this video, I'm gonna be showing you what exactly is prefetch_related in Django. We will demonstrate Django prefetch_related by making use of an example project which has two models and these models are related to each other using a ManytoMany relationship. We will show you what exactly is the use case of Django prefetch_related by querying the Django models and showing the corresponding SQL queries being executed in the background by making use of django debug toolbar. We will then explain the problems with the default accessing method and specifically about the n+1 query problem in Django ORM. Then, we will fix the n+1 problem using Django prefetch_related. We will also talk about the underlying SQL queries being executed if we use prefetch_related. At the end, we will also talk about the difference between prefetch_related and select_related, when to use Django select_related and when to use Django prefetch_related and so on. This video is a part of Django Relationships series.
    Subscribe | Code Band
    Django select_related : • Django select_related ...
    Django Debug Toolbar : • Django Debug Toolbar |...
    Django prefetch_related Docs : docs.djangopro...
    Medium article on Django prefetch_related : / django-select-related-...
    Other videos in Code Band:
    Django Autocomplete with jQuery UI | AJAX | Search | Filter from Database | Tutorial | Code Band : • Django Autocomplete wi...
    Build a Weather App in Django | Scrape Weather Data from Google | Beautiful Soup | Python Requests : • Build a Weather App in...
    Python .env File | Django .env File Example | Python Decouple Django | Django Environment Variables : • Python .env File | Dj...
    Django Dependent Drop Down List Example | Integration with Django Forms and Django Form Validations : • Django Dependent Drop ...
    Django Select2 Tutorial | Django Dropdown | Django ajax Dropdown | Django Search and Filter : • Django Select2 Tutoria...
    Django Shell Plus | Advanced Django Shell | Django Shell Commands | Django Shell Tutorial : • Django Shell Plus | Ad...
    Django Seed Data into Database Models | Django Faker | Django loaddata | Django Dummy Data : • Django Seed Data into ...
    3 Cool Django Features that Django Developers are Not Aware of : • 3 Cool Django Features...
    Build a Weather App in Django | Scrape Weather Data from Google | Beautiful Soup | Python Requests : • Build a Weather App in...
    Django Custom User Model | Email as Username | Phone as Username | Authentication Example |Code Band : • Django Custom User Mod...
    Playlist | Django Authentication | The easiest and actual way of doing it : • Django Authentication ...
    Playlist | Django Relationships | Explained with Example by Code Band : • Django Relationships |...
    Custom Model Manager & Model QuerySet in Django : • Custom Model Manager &...
    Render Django Form Manually : Make Custom Form with Form Validations - • Render Django Form Man...
    Generic Class Based Views in Django | Introduction & Create View in Django | Learn by Example | CRUD : • Generic Class Based Vi...
    How to Deploy Django Project on Heroku for Free : • How to Deploy Django P...
    Connect Django with MySQL : • How to Connect Django ...
    Instagram Automation using Python : • Instagram Automation u...
    Top 5 Programming Languages to Learn in 2020 : • Top 5 Programming Lang...
    :) You Can Find Me On:
    My Website - jasim.tech/
    Linkedin - / akjasim
    Github - github.com/akj...
    Facebook - / jasimakjasim
    Twitter - / akjasim
    Instagram - / jasim_ak

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

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

    thanks for making video as asked earlier
    note you are the only one who made a tutorial video on this

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

      Thanks for the support❤️

  • @raufp6
    @raufp6 6 หลายเดือนก่อน

    Thank you! It is fantastic video. i liked way that demonstrated the use case.After watched videos i was thinking that why didn't see your videos before.

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

    Amazing! Thank you for taking the time to explain this!

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

      Glad it was helpful!😊

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

    Nice . I was avoiding to learn these concepts from djangocon as there videos are very large. But you have helped me to learn what I needed.

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

      Glad to hear that! Keep learning😊

  • @sajidhaabdulla5486
    @sajidhaabdulla5486 2 ปีที่แล้ว

    It was so useful thankyou

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

      Glad to hear that😊

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

    Awesome way to demonstrate the concept 👍🏻

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

      Very glad to hear that...Thanks a lot😊

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

    that's great vid and easy to understand

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

      Glad to hear that😊

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

    very informative, thank you so much

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

      Glad to hear that😊

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

    Nice and clear video!

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

      Thanks a lot..glad it helped..keep supporting😊

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

    You are doing great videos brother. Keep going.

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

      Thanks a lot and keep supporting 😊

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

    Very helpful & Informative. One small suggestion, please avoid putting background music in your videos :)

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

      At least while you are talking

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

      Thanks a lot...❤️

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

      Sorry for the trouble...Newer videos doesn't have bgm...Thanks for the suggestion😊

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

      @@CodeBand thank you :)

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

      😊

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

    Awesome as always bro.

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

      Thanks❤️

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

    Thanks

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

      Welcome

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

    Very nice

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

      Thanks❤️

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

    Perfect one

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

      Glad to hear that...😊

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

    So useful, I share :)

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

      Awesome, thank you!

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

    that background music did't bothered much to me but if you control that volume little low it would be great i feel

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

      Sorry for the late reply...
      Yup, sure... for the next videos, I'll take care of it. Thanks for the suggestion.

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

    Great video, but the background music is very distracting when watching this.

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

    in prefetch_related, we are getting list of stores that contain any book as output, not book wise store list. Am i right ?

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

      I didn't exactly remember the case, but definitely the video tells it all...😊

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

    if possible make a video for pure graphql, graphene, graphene-django

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

      Great idea! Thanks😊
      I'm also planning to do a REST series. What's your view on this?

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

      @@CodeBand There are so many source for REST. if you have plan for that, no problem for us. Because your teaching always unique and understandable. But for graphql i think there are some limited videos. In my point of view i'm not understandable. Because they using graphql with node and react. so if possible make a clear video for graphql, graphene, graphene_django. without react and node. this is a follower request.

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

      @@vijayl2906 Definitely, will do that in the future bro...I always love to make unique videos, so, I will definitely turn to this one once I complete the pending videos/playlists. Thanks a lot bro for the support❤️

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

    Awsorm

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

      Thank yew❤️

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

    why did you use "store_set" instead of "books" like in documentation? books actually doesnt work, i dont know why
    but "store_set" works

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

      stackoverflow.com/questions/47619860/laravel-5-4-on-heroku-forbidden-you-dont-have-permission-to-access-on-this-s

    • @sujoydhar248
      @sujoydhar248 2 ปีที่แล้ว

      as its a manytomany relation, one book can be in multiple stores. store_set.all() fetch all the stores that has the book in it .

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

    what if we use .last() instead of .all() while looping?

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

      Did you mean in book.store_set.all() ?
      Anyways, last() returns the last object in the queryset and you can check how the query is generated using the same Debug Toolbar...

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

    you didnt say anythink new but de docs?

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

      I tried explaining the concepts using the docs, not exactly reading the docs...

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

    Please increase font size for this video bro. Not able to see the code ☹️

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

      I'm very sorry brother...It was my mistake and will definitely try to fix in future videos 😊

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

    Please increase the font size it is not visible.

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

      Definitely bro, I'm very sorry for that mistake...I will definitely take care of that in future videos.
      ❤️

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

    If we have three models
    class Author(models.Model):
    name = models.CharField(max_length=50)
    class Book(models.Model):
    name= models.CharField(max_length=50)
    author = models.ForeignKey(Author,on_delete=models.CASCADE, related_name='books')
    class Blog(models.Model):
    name= models.CharField(max_length=50)
    author = models.ForeignKey(Author,on_delete=models.CASCADE, related_name='blogs)
    if we are creating a detailed view of an Author (John), can we use the below querie to fetch all the Books and Blogs he has written?
    author = Author.objects.prefetch_related('books_set', 'blogs_set').get(name='John')
    for book in books_set.all():
    print(book.name)
    for blog in blogs_set.all():
    print(blog.name)

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

      First thing, if you specify a related_name, then use that..(books, blogs instead of books_set, blogs_set)
      And I'm not sure whether that query works perfectly, please do a quick run and let me know...
      Although, you can chain prefetch_related results...
      stackoverflow.com/questions/27116770/prefetch-related-for-multiple-levels

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

      @@CodeBand Thank you

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

      @@saimaruthi_1 Keep going👍🏻

  • @affanarif750
    @affanarif750 9 หลายเดือนก่อน

    When making an explanation video please remove background music it is very distracting

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

    anybody can help me which django function we can use for left join or right join.

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

      It's best to deal with what you are trying to achieve, Google and stackoverflow will definitely have answers😊

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

      @@CodeBand if you have just simply ping me out :)

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

      😊

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

    Music too loud

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

      Very sorry for that...

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

    the music is unnecessary

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

      Very sorry for that...

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

    Please don't copy and paste through Git . You are not explaining each and every line . Difficulty for understanding by Beginners

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

      Very sorry to hear that...and indeed, I'm not copying and pasting a single line of code from GitHub in this video, I try to explain every line of code in detail...Anyways, sorry to hear that, I would definitely improve my side in future videos, if anything goes wrong😊

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

      @@CodeBand one request please . I don't know Ajax and JQuery . I am trying to make a Chained Dropdown list . Is there is any other possible way . I like to keep it in Registration page . Is it possible I.e Custom Registration

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

      @@nagendranfriends3153 It's almost impossible to implement chained dropdown in Django without knowing AJAX and/or jQuery. You might need to check for Django packages that might do this for you without directly messing with AJAX. Check this website for Django packages:
      djangopackages.org/

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

      @@CodeBand oh , then please explain about the Ajax and JQuery . And why they are used . Please in a short video. Because of not having a bit knowledge about them it is difficult to use it . Please explain

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

      @@nagendranfriends3153 You can find quick crash courses of jQuery and AJAX in YTB. There are a lot of videos out there!😊