Це відео не доступне.
Перепрошуємо.

Django ORM - Querying and Creating Records / Working with Foreign Keys

Поділитися
Вставка
  • Опубліковано 18 сер 2024
  • In this video, we'll cover ways to query databases with the Django ORM, and ways to create data with the ORM.
    We'll look at a number of techniques, including the following methods:
    - Model.objects.all()
    - Model.objects.filter() / Model.objects.exclude()
    - Model.objects.first() / Model.objects.last()
    - Model.objects.create() / Model.objects.get_or_create()
    We'll also look at how to create and query Foreign Key relations in Django.
    ▶️ Full Playlist:
    • Django ORM Deep Dive
    📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
    00:00 Intro
    00:55 django-extensions runscript command
    02:21 Creating records by instantiating and saving Django models
    07:07 Querying Data - Getting All Rows From a Table
    09:41 Querying First Row with Model.objects.first()
    11:05 Indexing into Django QuerySets
    11:55 Creating Records with Model.objects.create()
    13:51 Counting records with Model.objects.count()
    15:15 Querying Foreign Keys in Django
    18:49 Filtering records with Model.objects.filter()
    21:18 Filtering records with Model.objects.exclude()
    22:16 Updating existing records with model save() method
    24:37 Querying related records in Django ORM
    26:20 Querying reverse relations in Django
    31:22 Getting OR creating data with Model.objects.get_or_create()
    ☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
    To support the channel and encourage new videos, please consider buying me a coffee here:
    ko-fi.com/bugb...
    𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:
    📖 Blog: bugbytes.io/po...
    👾 Github: github.com/bug...
    🐦 Twitter: / bugbytesio
    📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:
    Django-extensions runscript: django-extensi...
    Django filter(): docs.djangopro...
    Django - Following Relations Backward: docs.djangopro...
    #django #python #webdevelopment #database

КОМЕНТАРІ • 73

  • @bugbytes3923
    @bugbytes3923  4 місяці тому +2

    Playlist link: ua-cam.com/video/EsBqIZmR2Uc/v-deo.html

  • @anouarbagent2511
    @anouarbagent2511 Рік тому +18

    this is literally the best Django Channel there is, keep up the good work Lyle it will definitely get much bigger.

  • @552rajeshyadav2
    @552rajeshyadav2 Рік тому +3

    I don't get any channel regarding django orm since past few months but got an exciting series from heaven thank u❤❤❤❤

    • @bugbytes3923
      @bugbytes3923  Рік тому +1

      Thank you so much, really appreciate the nice comment! 🙂

  • @hiankitkr
    @hiankitkr Рік тому +2

    showing sql was great, it gives idea of what queries is being executed, keep doing it.

  • @sidds09
    @sidds09 Рік тому +3

    great. I don't think anybody explaining these things in django. most just touch the basic things. super helpful. just keep on doing.. . hope you also spend enough time on authentication and show custom user models in future.

    • @bugbytes3923
      @bugbytes3923  Рік тому

      Thank you - glad it is helpful :)
      Will do!

  • @AmoahDevLabs
    @AmoahDevLabs Рік тому +1

    It's always good to have you here. Thanks very much for the insights you've been given.

  • @seydinaoumarsamabaly1806
    @seydinaoumarsamabaly1806 Рік тому +3

    So simple, so easy to learn with you master 🙌🏾. Thank you.

  • @Davidkiania
    @Davidkiania Рік тому +1

    Thank you so much for this tutorial and of course very interested in this content and please continue in this very format there's very little internet content on the same. Excellent stuff

  • @silkogelman
    @silkogelman Рік тому +2

    This is a great tutorial about querying the database with Django! 🙏😀👍
    I like that you show the under-the-hood SQL queries sometimes (and not too much).
    And as pprint alternative: the rich and django-rich libraries are great for pretty printing stuff to the terminal too.

    • @bugbytes3923
      @bugbytes3923  Рік тому +1

      Thanks a lot Sil!
      I'll look into rich/django-rich - haven't used those tools yet.

    • @silkogelman
      @silkogelman Рік тому

      @@bugbytes3923 With rich I love that it requires zero code change as drop-in replacement 'from rich import print'. Done.

  • @Ego_Maritm
    @Ego_Maritm Рік тому +2

    As always, great, high level, and detailed videos. Thank you so much.💪

  • @carstenthies9597
    @carstenthies9597 Рік тому

    Excellent video series so far. A bit fast but that keeps you focused. Not exactly for people with a moder super short attention span. Thank You so much for putting this together, learning a lot.

    • @bugbytes3923
      @bugbytes3923  Рік тому

      Thank you! I'll take the suggestions on board, too! Thanks for watching.

  • @iwswordpress
    @iwswordpress 8 місяців тому +1

    another excellent video! Channel is probably the best for Django and related subjects.

  • @iiju8212
    @iiju8212 Рік тому

    I haven't seen the video yet. But I am grateful that you are putting such videos out.

  • @LegionLeague
    @LegionLeague Рік тому +1

    Please keep going with this series. Like from me on every single video here. This is amazing.

    • @bugbytes3923
      @bugbytes3923  Рік тому

      Thanks a lot! Really happy the series has been useful - got around 15 more videos written up, so plenty more to come!

  • @ffzz007
    @ffzz007 Рік тому +1

    Thanks Alot... You are my Django Mentor

  • @zxmu5755
    @zxmu5755 Місяць тому +1

    ¡Gracias!

  • @frameff9073
    @frameff9073 Рік тому +1

    Good thank

  • @Surajkumar-ok4ze
    @Surajkumar-ok4ze 4 місяці тому

    Awesome explanation

  • @shamshad-live
    @shamshad-live Рік тому +1

    Perfect explanation !

  • @ytouate
    @ytouate Рік тому +1

    Very Great content, Just keep doing the queries explanation and Thank you

  • @zxmu5755
    @zxmu5755 Місяць тому

    Thank you so much!

  • @ammaryasser4839
    @ammaryasser4839 6 місяців тому

    Go on, if you make a complete course on Django for beginners it will be very useful and it will be a unique explanation

    • @bugbytes3923
      @bugbytes3923  6 місяців тому

      I'm looking forward to doing this at some point - just need to find the time!

  • @SOHAILKHAN-iu8fu
    @SOHAILKHAN-iu8fu Рік тому +1

    Awesome video 💪 plz Also make video on django subquery

    • @bugbytes3923
      @bugbytes3923  Рік тому

      Thank you! For sure, I'll do one on this later in this series.

  • @Just4Pro1
    @Just4Pro1 3 місяці тому

    i think we shouldn't use get_or_create for user rating beacuse if user previously gave 5 star to a restaurant and now it's dissatisfied and want to give 2 stars then get_or_create will not be able to find the 5star rating of the user and will create another record in Ratings table with 2 star rating and it will lead to duplicates in rating unless user field is defined as unique
    thank you so much for your effort, great series

  • @batuhanunal9380
    @batuhanunal9380 7 місяців тому

    Wonderful

  • @olable9
    @olable9 10 місяців тому

    I'm definitely interested

  • @nicenaija9952
    @nicenaija9952 Рік тому +1

    I see you’ve reviewed django-hijack. Have a look at Django-unfold too

  • @MayasMess
    @MayasMess Рік тому +1

    Awesome video 💪

  • @TomislavMiletic
    @TomislavMiletic Рік тому

    Thank you, 3 lessons in and already better than paid udemy orm course, straight to the point without losing time on theory. Is there an option to pay something in order to suggest, and you maybe take into consideration to create a video on that subject?

    • @bugbytes3923
      @bugbytes3923  Рік тому

      Hi Tomislav - thanks a lot for your support! You are free to suggest anything here, thank you! If you can support in any way (no obligation of course), there is also a ko-fi page:
      ko-fi.com/bugbytes
      Thanks again and glad you're enjoying the series! I have actually just this second finished recording the next video in this series - will be released this week.

  • @studioteque
    @studioteque Рік тому

    Would love it if you would do a video on creating a multi-tenant Django app. For example a SaaS that allows for organizations with multiple related users.

  • @amine63404
    @amine63404 6 місяців тому

    Great tutorial. I have a question please.
    When you have three tables as following :
    Restaurant
    Sale - fk restaurant
    Invoice - fk sale
    How do you get all invoices related to the restaurant ?
    Can we use something like
    Restaurant.sale_set.all().invoice_set.all()
    ???
    Thank you again for such a great tutorial.

  • @ohbhaibadiya
    @ohbhaibadiya 7 місяців тому

    This made Django learning very simple.. I just had one doubt, in normal query even when you were doing all the sql query has limit 21, did you configure that somewhere or is it default or where is that coming from? Thanks again for videos.

  • @jedi2light
    @jedi2light 3 місяці тому

    Isn't it confusing that UPDATE statement on save() method call contains all the fields whereas we only changed `name` of the restaurant? 23:56
    Just in case if someone is wondering how to avoid it, we can pass `update_fields` to `save()` methods which a list of strings where each string is the name of the field we want to include in `UPDATE...` statement produced by Django ORM

  • @raffritter6766
    @raffritter6766 Рік тому +1

    Thank you very much! 👍 Where would you put that logic? Do you use fat models?
    Btw. I'm interested in sql queries though I'm glad the ORM does the heavy lifting.

    • @bugbytes3923
      @bugbytes3923  Рік тому +1

      Thanks!
      I put a lot of the logic in models usually. Or use a custom manager or queryset implementation - more on that later in this series!
      Generally, in a real app, I try to keep the views thin, with different techniques for doing so.

    • @raffritter6766
      @raffritter6766 Рік тому +1

      @@bugbytes3923 Awesome, I can't wait to see that in action!

  • @Zzz-xt6ki
    @Zzz-xt6ki 4 місяці тому

    I am not getting suggestions like rating.object.create and rating.object.filter(). How to get those?

  • @user-xu7jz4li7e
    @user-xu7jz4li7e Рік тому

    First!

  • @pedarayududola1672
    @pedarayududola1672 4 місяці тому

    I'm getting the error ModuleNotFoundError: No module named 'django_extensionscore'

    • @bugbytes3923
      @bugbytes3923  4 місяці тому

      Make sure you have django-extensions installed (you can install with pip)
      It’s concatenating that with the “core” app - are you missing a comma between the two elements in INSTALLED_APPS?