How Model Queries Work in Django

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ส.ค. 2024
  • Queries are important in any web development framework because you'll be pulling in data for your app to work properly. In this video I show you how to write queries in Django.
    Need one-on-one help with your project? I can help through my coaching program. Learn more here: prettyprinted....
    Download the code here: s3.us-east-2.a...
    Join my free course on working with the database and models in Django: prettyprinted....
    Get the Django cheat sheet here: prettyprinted....
    Twitter: / pretty_printed
    Github: github.com/pre...

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

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

    Join my free course on working with the database and models in Django: prettyprinted.com/djangodata

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

      Hi, You have a mistake:
      Slice[from : to : Step] - this is the correct Slice

  • @Kamel419
    @Kamel419 6 ปีที่แล้ว +12

    Thanks for all of your videos Anthony, great work as always! I've never understood Django's ORM

  • @xtremehackerzpro9511
    @xtremehackerzpro9511 6 ปีที่แล้ว +12

    It's would be nice to mention that exist OR statement in filtering over object for example: result = Item.objects.filter(item.creator = owner) | Item.objects.filter(item.moderated = False)
    Also great way in Django is a extracting year or month of date attribute for example: result = Item.objects.filter(date__created__year='2018')
    Keep going with great tutorial :)

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      Yeah, I completely forget about OR queries. I guess I'll have to cover them in a future video. Same thing with dates.

  • @blakef.8566
    @blakef.8566 5 ปีที่แล้ว +3

    Thank you, much better than reading the documentation.

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      You're welcome! Thanks for watching.

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

      Blake F. Reading the documentation is effective when you know the basics, the documentation is for advanced things

  • @Ccviewcom
    @Ccviewcom 4 ปีที่แล้ว

    pretty smooth for a brazillian like me got easy to understand and pretty complete as you said!!! thank you so much sir, ill definitely recommend for coworkers and try the free course you said. Awesome!! keep going calm like this!!

  • @jomarsalazar2572
    @jomarsalazar2572 4 ปีที่แล้ว

    Been looking for solution on how to combine tables in django for 2 days already. And I thank God i found your tutorial

    • @jomarsalazar2572
      @jomarsalazar2572 4 ปีที่แล้ว

      can you help me pls how to combine 3 models?

  • @rajaryan7566
    @rajaryan7566 4 ปีที่แล้ว

    sir you are the best teacher

  • @marthavanorshoven1942
    @marthavanorshoven1942 4 ปีที่แล้ว

    This was VERY HELPFUL to me. I am new to Django and was wrestling with the query functionality

    • @prettyprinted
      @prettyprinted  4 ปีที่แล้ว

      I'm glad it helped. Thanks for watching!

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

    Thanks Pretty Printed! Very useful introduction for me. Love the ORM compared to raw SQL :)

  • @janekmuric
    @janekmuric 6 ปีที่แล้ว +24

    The x[5:10] syntax simply means "get items from 5th index to 10th index". I don't know why you complicated it so much.

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

    Very clear and concise explanation , thank you !

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

    DO YOU HAVE VIDEO FOR QUERIES WITHOUT MODELS?

  • @WaradeRahul121
    @WaradeRahul121 4 ปีที่แล้ว

    Thank you. I appreciate your effort. 30 minutes back i was unsure how to understand querying a model in easy ways, you just :D ! Respect :)

  • @user-rx3eh8bb4x
    @user-rx3eh8bb4x 4 ปีที่แล้ว +1

    This video really helped me out a lot. Thanks!

  • @olliewelsh123
    @olliewelsh123 4 ปีที่แล้ว

    simple, clear and useful to beginners. thanks!

  • @danielm3977
    @danielm3977 5 ปีที่แล้ว

    Pretty Printed , briefly. It was awesome and well-articulated. THANk U

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

    Thank you for the great explaination!

  • @bijarnia_vikash
    @bijarnia_vikash 6 ปีที่แล้ว

    Django's pick up lines( easy way to get records) works well on Models. Thank you It was great.

  • @codewithkg9096
    @codewithkg9096 4 ปีที่แล้ว

    Something I was looking for from long time!! Precious!

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

    How do you return a table value from a another user select table value. For instance returning firstName when selecting LastName from a People table. The value returned from the user selected lastName would populate a select field in a form. As well as the char field to hold the firstName after the user selects the lastName.

  • @suburbanblackboyradio2663
    @suburbanblackboyradio2663 4 ปีที่แล้ว

    Awesome vid. I am just having an issue with using the & query. OR works just fine, but & is returning nothing and I'm stumped.

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

    Hello Sir...!
    I have two models A and B which having many to many relation and form third model E with extra attributes .
    so i want to perform following sql query using django :
    select * from A , B , E where A.id = E.id and B.id = E.id and A.id = '107';
    how i can do..?
    A.objects().filter(e__aid=107)
    geves me only content from A model.
    Plzz Help..?

  • @nick_way
    @nick_way 4 ปีที่แล้ว

    Very clearly explained, thank you

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

    Thank you Sir. You are the best.

  • @Elmaco7
    @Elmaco7 4 ปีที่แล้ว +5

    Hi pretty printed. How can I create queries and view them on the site(when I do runserver) and not on the terminal's shell?

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

      You need to put the query into your template.

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

    very helpful stuffs . THANKS A LOT !!!

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

    Very usefull stuff. Thank you!
    Please add query's using "in" fuction.

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

    dang bro ur awsome thank you for all your content

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

    sir how to create model objects inside the code itself, not using shell or the admin panel

  • @StevenSmith68828
    @StevenSmith68828 4 ปีที่แล้ว

    Can you specify types? Like say that that age must be an int instead of a string

  • @ChaosArtist
    @ChaosArtist 5 ปีที่แล้ว

    Nice overview, very helpful to see the most common Django model queries.

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

    Pretty good info bro .. you have really helped me a lot in my personal project.. your videos are actually really informative n helpful..!!!

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

      I'm glad my videos have helped you. Thanks for watching!

  • @darkdraconium
    @darkdraconium 4 ปีที่แล้ว

    This was super helpful, thank you!

  • @AliHasanov
    @AliHasanov 4 ปีที่แล้ว

    Probably, you forgot :
    First object: Model.objects.filter("name").first()
    Last object: Model.objects.filter("name").last()
    To get random objects: Model.objects.order_by('?')
    and Q objects for complex lookups
    Thanks for everything :)

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

    Thanks bro such a helpful video!

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      You're welcome! Thanks for watching.

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

    Why objects.all() not giving all columns ?

  • @yashwardhan9014
    @yashwardhan9014 4 ปีที่แล้ว

    Is it possible to join two tables without using foreign key

  • @KrishnaManohar8021
    @KrishnaManohar8021 4 ปีที่แล้ว

    can you tell me how DateTime field u did?

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

    Hey, How we can query related to joins in django

  • @jafarhussain1304
    @jafarhussain1304 4 ปีที่แล้ว

    how to get the id of the particular object for example i need the id of python in the language model can we get it directly if we can what is the syntax for it can u plz help me out

  • @180doman
    @180doman 6 ปีที่แล้ว +1

    It would be great if you publish .sql file of your bases as well. Or fixture. Offcourse filling them by hand can be good excercise but in case of Programmers table it can be quite daunting task. Other than that - awesome tutorial!

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      I forgot to add the code link. You can download it here: s3.us-east-2.amazonaws.com/prettyprinted/django_model_queries.zip

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

    We still using Postgres. Can you provide some output file that we can duplicate your DB ?

  • @junaidtariq3265
    @junaidtariq3265 4 ปีที่แล้ว

    I have three table a,b,c. A have many to many relation with b and b have many to many relation to c. I have problem when I click on a its show all relative b but I want when I click on a it will show b table entries and all c entries which relative to b

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

    Thank you. Very helpful.

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      You're welcome! Thanks for watching.

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

    Can someone tell me where to find all these filter keywords (in the documentation)?

  • @nowyouknow2249
    @nowyouknow2249 4 ปีที่แล้ว

    Very useful
    Thanks

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

    In my project. Objects.all() is throwing error
    Please help

  • @harshraj22_
    @harshraj22_ 4 ปีที่แล้ว

    Thanks A Lot !
    The video was very helpful 😁😊
    Do you have any video explaining more about foreign keys ? It seems little confusing ☹️

  • @sumanbanik3339
    @sumanbanik3339 6 ปีที่แล้ว

    if its [:5], it simply means get the first five elements of the list, that is; 0,1,2,3,4....exclude 5, same with [5:10].

  • @ROMAN-zu3wu
    @ROMAN-zu3wu 2 ปีที่แล้ว

    Thank u so much bro

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

    Your videos have been a great source of information. Actually I need to query a foreign data wrapper (foreign table) of my Postgres db using Django. Is there any way I could do that.
    Any leads would be highly appreciated.
    Thanks!!

  • @MindJargon
    @MindJargon 4 ปีที่แล้ว

    Hi. Would using model queries and using CRUD MySQL methods be different? Thanks

  • @shashidhardevraj
    @shashidhardevraj 5 ปีที่แล้ว

    Thanks for the video! Its very clear.

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

    thanks Great video

  • @AjaySharma-jv6qn
    @AjaySharma-jv6qn 4 ปีที่แล้ว

    Thanks for your work

  • @mrmuranga
    @mrmuranga 4 ปีที่แล้ว

    well explained...thank you

  • @shadiismailalzouabi1035
    @shadiismailalzouabi1035 4 ปีที่แล้ว

    lPlease sir, python.programmers_set this command create many to many relationship between programmers module and ( programme language ) module?

  • @santoshkumar-xq6wx
    @santoshkumar-xq6wx 4 ปีที่แล้ว

    Nice vedio, thanks for making our work more easy, can you say how to user "OR" filter here.

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

    where can I find all filters for queries

  • @aniketmlk6
    @aniketmlk6 5 ปีที่แล้ว

    Thanks!
    Extremely High quality content

  • @AnimatedHooman
    @AnimatedHooman 6 ปีที่แล้ว

    Amazing video sir. Very informative

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

    Great video thanks Anthony

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

    It's a very good video

  • @pranavkhandelwal8474
    @pranavkhandelwal8474 5 ปีที่แล้ว

    Simple and Good Explanation :)

  • @saharamanson1970
    @saharamanson1970 4 ปีที่แล้ว

    thanks bro..

  • @manishkumar-db5xm
    @manishkumar-db5xm 5 ปีที่แล้ว

    Very useful video, Thanks

  • @JS-lw4ks
    @JS-lw4ks 4 ปีที่แล้ว

    Does anyone have any resources on connecting to an MSSQL database? I can connect to the DB and run a stored procedure but struggling with parameters

  • @pedrofolch9786
    @pedrofolch9786 6 ปีที่แล้ว

    Thanks Anthony, this is very helpful, I need to know if and how can we save the results of the query into the same model or another model? Could you provide an example, Lets say using boolean field and a list of Integers to save the average of the list, if the boolean is True. Or do you have another video with that kind of example?

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      If I understand you correctly then all you'd need to do is check if that boolean is true before saving the model. If it doesn't true, then you don't have to do anything.

  • @santanasta82
    @santanasta82 6 ปีที่แล้ว

    Great video again, Anthony! Thanks for your videos! Please keep on doing! One doubt: what is the program you use to see your database tables? Is that JADE? Where can I get it? I like the way you check your database. Thanks man! Congratulations!

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

      Yeah, that's JADE. You can get it in the Chrome web store here: chrome.google.com/webstore/detail/javascript-based-database/bponbdjkefbmgkfiiphhabghkkfocook?hl=en

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

      Perfect! Thank you very much! I love it!

  • @GraceandWisdom
    @GraceandWisdom 5 ปีที่แล้ว

    Just curious. What software app is being used for your command prompt? The one with the Reddish-Orange circle.

  • @harshthakkar8081
    @harshthakkar8081 4 ปีที่แล้ว

    can we use startwith = '[a-z]' like this ??

  • @HawkwardPause
    @HawkwardPause 6 ปีที่แล้ว

    Hi Anthony, can you filter from an offset all the way to the end of the table?
    i.e:
    Programmer.objects.all()[5:]
    in order to get the remainder?
    Thanks for these videos, they really help me focus down on the basics and remove roadblocks while I get used to Django.

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      If I understand you correctly, then yeah you can. An offset just skips the first n records. Limits to restrict the number of results, but with no limit, you'll get everything to the end.

  • @dziurappa
    @dziurappa 6 ปีที่แล้ว

    Hi Anthony ! Great tutorials ! Thanks a lot ! I have a problem which I can't solve by myself since I'm a beginner. For a example I'm making a website with some articles about dogs and cats. My question is: How to make some kind of button where USER (not admin) can select all the articles about dogs only. Where (which file) should I make a function with sql logic and how should I use it in .html ? Thank you in advance. Paul

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      You can start by creating two views. One for cats and one for dogs. In each view, you'll query for the category you want and return the results to some html template. Once you get that down, you should see the similarity between the two and you can think of ways you can combine the two views into one.

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

    mypost=post.objects.filter(id__topboard=self).count()
    print(mypost)
    return mypost
    i want to get acount all post for this board get Error like this >>>>
    ValueError at /
    Cannot query "Schoole": Must be "topic" instance.

  • @naimurrahman2367
    @naimurrahman2367 5 ปีที่แล้ว

    can anyone help to solve this
    ImportError: cannot import name 'get_user_object' from 'django.contrib.auth'

  • @mostafizurrahmanshohan9075
    @mostafizurrahmanshohan9075 6 ปีที่แล้ว

    Thank you... can u show us how we can make class-based URL slug?

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      I'll add that to my list. Thanks for the idea.

  • @RizwanAli-bu5oy
    @RizwanAli-bu5oy 6 ปีที่แล้ว

    how can i retrieve database tables and all the the data from mysql. if you like, make me a video on mysql, i'm using xampp server. please give me all the details about mysql and how to query data. i have made database using phpmyadmin, now i want to use that data in django app

  • @deepakprakash8940
    @deepakprakash8940 5 ปีที่แล้ว

    Hey great and nice video for basics but how you were clearing the shell so quickly was that some trick or shortcut plz share

  • @azamatuzbekov117
    @azamatuzbekov117 5 ปีที่แล้ว

    I'm wondering when we upload data to db should we enter ID or name itself? For example Programmer table column Language. Do you have any videos related to this?

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

      It depends on your situation. For the most part, I like IDs better, but names can work too if they're unique.

  • @annettemaza3922
    @annettemaza3922 5 ปีที่แล้ว

    Hi there! Thanks for the video! Is there a way to filter by next id? If I want to display the name of my next model on the current one? :)

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      I've never done something like that before. My approach would be to do it manually. Maybe this well help you: stackoverflow.com/questions/6021694/getting-next-and-previous-objects-in-django

  • @rvenu8932
    @rvenu8932 5 ปีที่แล้ว

    super brooooooo Exlente

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

    works the same, if i use filter.name, instead of filter.name__exact. so why not just use name??

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      You don't have to. I'm not sure why I did it here. Sometimes I get tunnel vision.

  • @neelesh.vishwakarma
    @neelesh.vishwakarma 5 ปีที่แล้ว

    I'm getting an error in the following expression
    Message.objects.filter(ishandled == True)
    'ishandled' is a field in the Message table and I'm getting error that ishandled is not defined. can you please help?

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      One equals sign instead of two.

  • @keerthivasang1178
    @keerthivasang1178 6 ปีที่แล้ว

    Thank U very much sir!!!!!!!!

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      You're welcome! Thanks for watching.

  • @harpalsinhjadeja2568
    @harpalsinhjadeja2568 5 ปีที่แล้ว

    how am i supposed to run sql queries in shell like lets say i want to insert then how do i insert something in the table using sql in django shell

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      Here's more information on that: docs.djangoproject.com/en/2.1/topics/db/sql/#executing-custom-sql-directly

  • @BrunoAnjo77
    @BrunoAnjo77 6 ปีที่แล้ว

    Thanks!

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      You're welcome! Thanks for watching.

  • @shiv77patil
    @shiv77patil 6 ปีที่แล้ว

    can u help me i querying foreignkey

  • @xtremehackerzpro9511
    @xtremehackerzpro9511 6 ปีที่แล้ว

    Great video again... Go ahead :)

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

    any Project with django sir?

  • @jarettchaiken1492
    @jarettchaiken1492 6 ปีที่แล้ว

    name__exact and name__iexact are doing the same thing for me.
    >>> Language.objects.filter(name__exact='Java')
    >>> Language.objects.filter(name__iexact='Java')

  • @as-youtuber6215
    @as-youtuber6215 5 ปีที่แล้ว

    Any video regarding django ORM joins????

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      Not yet, I'll consider making a video on that.

  • @Smoonwalkerm
    @Smoonwalkerm 6 ปีที่แล้ว

    When I type Company.objects.all().. I'm getting this:
    ....Why not the names of companies ?

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

      You have to define __str__ in your model class.

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

    Grt ❣️❣️❣️❣️

  • @KaranKumar-wb5bn
    @KaranKumar-wb5bn 4 ปีที่แล้ว

    I love you🖤

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

    good

  • @kakumbanassirissa3972
    @kakumbanassirissa3972 5 ปีที่แล้ว

    Thanks for the video
    i however tried to filter a column from one of my models which has a foreign key but i had to reference it with the primary key instead of the real word. is there a way i can look up for something with out using the primary key. for example Programmer.objects.filter(company__exact='Microsoft') - returned the error but Programmer.objects.filter(company__exact='1') where 1 is the primary key Id for microsoft return no errors how can i make the first statement work. Note: i just used the above example thanks

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

      You would use __ to get into the child model. So company__name__exact='Microsoft'

    • @kakumbanassirissa3972
      @kakumbanassirissa3972 5 ปีที่แล้ว

      ​@@prettyprinted Thanks will try that and revert. do u have any videos on writing templates?

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      @@kakumbanassirissa3972 Check out my Jinja playlist. It's for Flask, but it's very similar to the default Django template language.

    • @kakumbanassirissa3972
      @kakumbanassirissa3972 5 ปีที่แล้ว

      Pretty Printed thanks

  • @BehruzbekOtayev
    @BehruzbekOtayev 4 ปีที่แล้ว

    Why do you call it 'Jango'? Its with D 'Django'.

  • @mohaamiin8767
    @mohaamiin8767 6 ปีที่แล้ว

    1:18 can you tell what that app is and what is used for plz

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      JADE. It's in the Chrome Chrome app store.

    • @mohaamiin8767
      @mohaamiin8767 6 ปีที่แล้ว

      @@prettyprinted thanks can you make video how to use

    • @prettyprinted
      @prettyprinted  6 ปีที่แล้ว

      Here you go: th-cam.com/video/J98ItU1jRq4/w-d-xo.html

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

    20:40 hahaha :D

  • @nickt423
    @nickt423 5 ปีที่แล้ว

    I didnt watch the full video because some reason this is hard to follow along without the IDE.
    views.py, models.py, ect. dont matter here?

    • @prettyprinted
      @prettyprinted  5 ปีที่แล้ว

      The video covers the basics of the queries, so I use the console instead of the web app so you can see the results instantly. But when you're creating a real app, you'd use the same statements within your views.py file as you do in the console here.