Welcome back, everyone, to this lecture on reading and querying with your database.
Each model you create comes with a manager that allows you to create what's known as a query set, which
can then be used to retrieve entries from the database.
Keep in mind that the query set is actually lazily evaluated, meaning that it doesn't hit the database
until it's explicitly asked to grab the information.
Which is nice because you don't want to ping your database for every extra filter that you add on to
your query set.
This will make more sense when we get into more advanced filtering techniques.
So recall that previously we use something like my model, the objects in order to create or bulk creates
entries.
This is known as the Django model manager or just manager for short.
This manager can then actually read the database through the use of different method calls in the same
way it could create entries also through the use of method calls.
There are the call and get method calls to actually retrieve and get the information from the database.
And then you can also narrow down results with method calls like that filter and that exclude.
And what's nice about this manager and the query set is just putting in that filter in that exclude
do not explicitly call the database.
It's not until you actually call for something like it, thought all or thought get that the database
is hit with the request.
So in this particular election, we're really just going to focus on the Dot all method, which allows
us to grab all the entries in the database table.
Typically, we won't want everything in a database table, so we'll need to filter our results, but
we'll discuss filtering in more detail later on in the next lecture.
For right now, we're really just exploring how do we grab everything from a table and what does that
information actually look like?
A general great resource on queries of examples can be found at Jingo Project com under the DB or database
section under queries.
That's really where we're referencing for the next series of lectures.
For right now, we want to explore the very basics of reading and data that is all the entries in a
tables entry points.
And then what changes we can make to make the model actually have results that are more human readable.
So let's head over to the code editor.
OK, so here I am back inside Visual Studio code.
I'm going to launch my shell again.
Which remember is Python managed the pie shell.
Go ahead and enter there.
And again, because I'm using interactive python.
If I python, because that's how I configured things in my computer, I get to see the input output.
But yours may just be just a plain python shell.
That's totally OK.
So.
Remember, I want to actually get access to the patient model, which means I need to import actually
say from.
And then office, the models, import patients.
OK, so now I have patients available to us.
Now we're going to do is we're actually going to query this database and see what happens.
So the simplest way to do this is by simply saying patient objects.
And this is the manager.
The manager can then actually get a query set, and the simplest query is to simply just request give
me all the objects or entries in this particular table or database.
And this returns back a query set that looks a lot like a list.
So you get back this query set.
It is kind of like a list in the sense that you can actually index things from this.
So for example, if you only wanted, let's say, the first item, you could put brackets there for
zero and then it would just return back the first patient.
So notice it does have some sort of primary I.D. or primary care.
One, two, three or four or five, et cetera.
But you're probably wondering, well, what's the deal?
Why is there more information about the first name, last name or age?
Well, recall appear in our model.
There is no human readable screen representation of this class.
And in fact, you would get this for any object you attempted to print out in Python that didn't have
a string method attached to it.
So let's add that in so that it's more readable for us.
We're going to say the E.F. underscore underscore S.T.A.R. underscore underscore percent self.
And now we're just going to return whatever the string representation we want for this particular model
object this.
So I'm going to use string literal formatting here.
And let's say that I want to report back the last name, first comma, first name again.
It's kind of up to you.
So we'll say solved that last name.
And then I'm also going to say soft first name, and let's put a comma here.
And let's say is and then let's report their age, self-taught, eight years old.
OK, so this is kind of a long string representation, but hopefully you get the idea.
You can obviously edit this to whatever's most convenient.
Not sure if it's really necessary to say their age when we report back patients, but you get the idea.
So I'm going to save that change in models that pay in order to register these changes for the ST method.
I do need to restart my shell again.
Otherwise it hasn't actually registered the changes to the patient class you imported.
So what I'm going to say is, again, Python, after you've quit out of the other shells, remember
to quit out of the shell is simply quit open close parentheses.
But now I'm going to restart it with Python managed the pie shell.
There we go.
And so let's import again from.
Office, not models.
In poor patient.
And then we're going to say again, patients, the objects.
All enter and not your see, your query is a lot different, says Smith Khalil's, 30 years old.
And again, you could just grab the very first one if you wanted to see something like you have an instance
from the patient table or a patient model.
And then Smith Carl is three years old.
OK, so that is how we can grab everything from a database table or just grab particular instances as
well as slicing, so you could also slice with index notation.
So, for example, I can go zero all the way up to, but not including index two.
And I see a query set with two patients.
Typically, we don't want to just grab everything and then index off of that.
Instead, we want to filter and then get particular entries.
So let's talk about that in the next lecture.