Django ORM Primer - Django QuerySet API and QuerySets

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this tutorial is part of our Django database  RM Mastery course where we learn how to master   building and interacting with databases within  a Django project you can watch the entire course   uninterrupted ad-free and access updated tutorial  source code and other course resources from our   udemy course alternatively you can access the  course YouTube playlist featuring tutorials from   this course links to our udemy course and YouTube  playlists can be found in the video description   this tutorial is not intended to dive  deeply into the subject matter we hope   to introduce and take the first steps  priming you with underpinning knowledge   to give you the starting point to  explore the Django query set further   in this tutorial we're going to try and  outline the purpose and role of the query set   a quick recap so far we know that we start by  building Django models as a Class A model class   represents a database table to return data from  the database for example we require the query set   API method which can be accessed via the model  manager so before we delve into query sets let's   ensure that we have a basic understanding of  a query in the context of relational databases   let's think of a query as a means to communicate  with the database backend we send queries to   the database backend which reads the query and  performs the action writing a query requires a   query language known to the database so that  it can actually understand the instruction   the standard query language for database  management is structured query language SQL other   query languages do exist but in general and in  the world of Django and relational databases SQL   will be used now as I mentioned Django the Django  RM provides developers with a non-sql approach in   that we can write database actions or queries  in Python not SQL although this is true for the   developer importantly behind the scenes abstracted  away from view Django will be converting our   python code into SQL before then sending  the query to the database so it can be read   and then actioned by the database so this  image depicts the fact that we utilize the   model manager to access the query set API we  generate our equivalent queries in our Django   environment and then this will then get converted  into a natural SQL statement which can then be   executed by the database and then  return the data for example if we   have requested any data from a database  return that back to our Django application   so we now have a query we know about a query which  is a way of us actually sending information to the   database so it can then perform the action so  let's talk a little bit about the query set API   now remember what's Happening Here Loosely  we are utilizing the tools in the query set   API to construct a a way of then creating an  SQL statement which will then be executed by   the database to perform the action that we need  within our application so the query set API allows   the Django developer to write database queries  without learning SQL to interact with the database   it provides a range of different methods  to create return update and delete data   and other refinements to create specific query  operations or actions for our database to execute   if you type in Django query set API into Google  typically the top option will be the Django   documentation detailing the query set API so what  you're going to find here in the documentation is   a list here of all the methods here on the right  hand side so these are all the methods here that   we can utilize to generate or create queries  or instructions for the database to perform   so there's a number of different instructions  here as well as different refinements that we   can utilize to really fine-tune our query  to return specific data from the database   so we saw an example or I've been providing an  example utilizing the all method here so let's   take a look at that in action so let's put the  query set API into action here we have a simple   Django model don't worry we're not following  along step by step here just going to show you   this example so here we have a model here this  is a student table we have just one field which   is first name I've gone ahead and populated this  so I can access this table just show the table   so we have it looks like five different names  here first names or five different students   actually in our database so this is a this is  our actual database here it has two fields and   you can see the first names that we've entered  into our database so I've entered the python   shell here I'm going to first of all import the  resources that's the student table and then I'm   going to run a query to return all of the data  from the database so remember what we're going   to need here is the name of the model which is  student then remember we're going to need our   manager which is objects but notice there isn't  any reference here actually in the in our model   here to the objects model manager this is assigned  to each and every model so we know that behind the   scenes it's available so I need my model manager  here objects and then I can use one of the methods   from the query sets API so if we just bring  that documentation back in here so here we've   been showing the example earlier previously in  this mini series of the all method here which   should then return all the data all the fields  and all the data from the model that we specified   so let's give that a go so all so that's the  method that we're using here now press enter   and you can see here that we in natural fact  we're returning all the data from our database   so so far we have created a query without  utilizing any SQL and like I mentioned previously   behind the scenes Django is converting our code  that we generated to actually generate the data   from the database into SQL so let's take a look at  it behind the scenes Django is keeping a record of   the SQL queries that were executed by the database  as we have previously created a so-called query to   return data from the student table we can now  access that data so here I'm going to bring in   the connection resource and then that's going to  then give us access to the connection query so   we can view the query that was generated by the  action that we performed previously so if I were   to print the connection queries we can now see the  query query equivalent that was generated from the   action that we took previously so you may remember  we typed in student.objects.org to actually return   all the data from the student table so this is the  SQL equivalent that is generated and then passed   over to a database to execute our instructions so  a common operation that you're going to perform   on your data is that you're going to filter  out data so you're going to make refinements   on your query so let's for example we wanted to  return all the students where the first name was   equal to something so what we have here is the  filter function so we can utilize this to as it   says here it turns a new query set containing  objects that match the given lookup parameters   so let's go back to the code here so we know that  this here will return all the data so now we can   add in a filter so we don't need to specify all  here so we're just going to look at all the data   so we're going to create a filter so we're going  to say here that for example first name is equal   to and let's just specify a first name here but  let's give that a go and notice now instead of   returning all the data from the database we  specify data utilizing the filter function   here and returned all the students where the first  name equals Eli so again we can inspect that query   so let's uh print the queries and here we go so  it looks like we've selected the ID field and   the first name field from the test app student  database that was the first query so we can now   look at the second query here so same thing again  we selected the same field from the same table   but this time there is a where Clause so where the  student first name equals and then the string Eli   so hopefully that has given you a visualization  and idea of what's Happening Here We utilize the   Django query set API to create queries and you can  see how that enables us to fine-tune the query to   generate instructions that we're going to need  to return specific data from the database now   you can already see from the length of this SQL  statement how lengthy that can be in comparison   to what we actually have to type here in Django so  for example here this student objects filter first   name equals Ellie that's equivalent to for example  remember for example all of this code here so you   can see it's a lot quicker and a lot easier to  create queries than potentially writing SQL code   hopefully that example has given  you a better understanding now of   the knowledge that we learned previously  and about queries and the query set API   so hopefully now we have all the underpinning  knowledge to better understand the query set   if we take a look at the query sets API reference  here they separate the methods into really two   categories the first category are methods that  return new query sets and methods that do not   return query sets so here we're referencing  the fact that when we return data from the   database it's in a particular format so that  we can work with it within our Django project   so here the methods here that return query sets  are methods that actually extract data from the   database or perform some sort of interaction with  the database and returns data from the database   whereas the methods that do not return query  sets apart from get for example everything here   potentially performs an action on the database  that doesn't necessarily return any data from   the database so take example here create so here  we're creating data in the database so we're not   returning any data we're counting data for example  we're updating data so those type of operations   that we might want to perform where we're not  necessarily returning any data from the database   so generally we now have learned  that there are two types of methods   provided by the query set API those that  return query sets and those that don't   when a query set is evaluated that returns  data from a database a query set is returned   each row returned from the databases  represented as an object inside a query set   the query set is generally defined as a collection  of objects from the database so a query set is   just a type like any other python type if we  inspect a query set we find a query set type   so I've said quite a lot there now let me just put  that into context here so here we have a query set   which is going to return all of the students with  the first name Ellie but let's just uh and let's   just go for the all so we just return all the  data from our database so if we execute that   it's returning all the data now notice what gets  returned here are objects inside of a query set   now this query set is iterable so we can iterate  over it with the general and built-in python tools   like we would do iterating over any list for  example and we can extract this data from the   query set if we're not too sure we can utilize  for example type to inspect the type of data   so let's say x equals let's just run this query  again student.objects.org so we return that in   to our variable here and let's just go ahead and  print and then type we can inspect the type of The Returned object here and you can see here  that we return a query set so the type of data   that's been returned here or yeah is a query set  and we can see that the objects here represent the   individual rows inside of the in this case  the student table so we can visualize this   in our diagram here we have a query set returned  then each row inside of a student table assuming   that we've queried this table and returned all the  data each row here will be returned as an object   inside of our query sets and of course then we  can go into the query set utilizing our Python   language and extract the data from it so we can  Loosely now say that a query set is constructed   via the model manager is created utilizing the  query set API methods that return a query set   so although this diagram isn't technically correct  we can now see the bigger picture here the general   flow utilizing the Django orm we are going to  be creating a model that model creates tables   once we have tables we can insert data once we  have data we can then generate queries utilizing   the query set API so we've seen an example of  utilizing the all filter or sorry the all function   behind the scenes that's going to get  converted into an SQL statement which   can then be executed on the database and  data can then be returned through the orm   and a query set can be generated and inside  of our query set each row that's returned   from the database will then be converted  into an object inside of that query set   so our aim in this tutorial was to try and outline  the purpose and role of a query set so hopefully   you've got a better understanding now of not  only the query set but how we interact with   the Django query set API to generate queries  to return the query set so to try and summarize   the Django RM provides a convenient way  of extracting data from the database to   return data from the database we construct a  query set through the model manager data is   returned in a query set a query set represents  a collection of objects from the database each   record or row returned from the database will  be represented as an object within the query set
Info
Channel: Very Academy
Views: 7,687
Rating: undefined out of 5
Keywords: django ORM, orm mastery, django QuerySet, django QuerySet API
Id: tO1vvj70tX0
Channel Id: undefined
Length: 15min 7sec (907 seconds)
Published: Tue Feb 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.