Welcome everyone to this continued discussion of filtering query results in this lecture will focus
on a filter and get methods.
The get operation or method allows us to grab a single item from the model table.
Now this is typically reserved for something where you're pretty sure there's only one single unique
entry.
Otherwise, get actually returns an error.
For example, the default primary key that is automatically created by jingo, such as Piqué, is equal
to one, two, three or any other number, which has a unique index position row in that table.
That's typically what you see get used for.
If we want to further filter our results rather than just grab all or get a single item, we can use
the filter method to narrow down based on conditions and the filter methods can actually be chained
together.
Django also provides operators for query sets, which allows us to actually directly use logic like
an and operator or operator within a single filter call.
So these operators actually need to be imported from Django DB models from the queue function, and
we'll show you how to do that later on.
So what we're going to do is explore the following topics getting a single entry from our table with
the get method filtering down to get maybe a set that we're interested in of results and then how to
use operators in conjunction with filters.
Let's head back to our code editor.
OK, so here I am inside Visual Studio code recall that our model technically has three fields or attributes.
The first name, last name and h, but also remember that when we actually ran the initial migration,
jingo automatically creates an I.D. for this model, and the I.D. is a big auto field.
Auto stands for the fact that it's going to automatically increment that number each time you actually
put in a new row.
So it's going to be and then an +1 and +2 and so on.
So it starts at the number one, then two, the three, then four, etc. Big just means it allows it
to be a very large integer because jingo technically doesn't know how many entries are going to put.
So it allows us to be a very large integer number.
That means every time you actually put in a new data entry row into your table, there's going to be
a primary key or ID associated with it.
And the primary key is equal to true is what's associating the fact that this ID column or index is
actually the primary key as well.
This is super useful because now you have a unique identifier for each entry in your data table.
That means even if you have a patient with the same first name and same last name and same age as someone
else like Mike Smith age 30 and you happen to have two patients with that, you can uniquely identify
them by their I.D..
You can think about this in the same way as your driver's license has a unique ID number or your medical
insurance card or your Citizen's ID.
All have unique ID numbers that we don't get confused if you happen to share the same first name, last
name and birthday as someone else.
OK, so the fact that we know I.D. is a unique identifier then allows us to use get.
So we're going to do here is, say, patients that objects that get and then I can say piqué or primary
key and then set the sequel to something like one.
Keep in mind in sequel, the indexing starts at one, not zero, like in Python.
So if I want the very first item that I ever entered, I would say Piqué is equal to one, and then
I would get back my patient.
Smith or Carl Smith is three years old.
Remember, Carl Smith was the very first patient I put into this table.
You should also note that it is only designed to grab the single item that can be retrieved, and it's
actually going to report back an error if you try to grab more than one item, and we'll show more on
that later.
For example, maybe I say patient objects doc get in.
Imagine I tried to put some sort of filter in this like first name equal to Smith because I know there's
more than one there.
If I try to do something like this, this is going to give you an error and it's going to say, Hey,
that doesn't exist.
And the fact that there is no first name was Smith.
That's one possible error.
This patient just doesn't exist.
Another possible error is if I say last name Smith.
So note here, I'm saying last name Smith, which I know is actually something in my database.
Then I get back another error.
Multiple objects return.
And this is key.
The get method can only return a single object, and that's specifically designed to make sure when
you're using get, you should be using it in a place where you only expect a single patient or a single
data row to be returned, something like primary key.
And it actually has a really nice error Message says, Hey, this returned more than one patient and
it actually says an irritant.
Three.
So this is basically telling you you should probably be using all again with some sort of filter attached
to it.
So let's talk about filters because you are not going to typically be running a filter like this.
Instead, you actually do a filter call.
So we do the following.
Let me clear this I'm going to say patient the objects and then I call a filter.
And inside this filter is where I can begin to do things like Boolean statements, so I can say something
like, OK, filter, where the last name?
Is equal to Smith.
Notice I'm doing all lowercase Smith here.
And you're going to have match access to whatever data entries you put in as well.
So then instead of saying get because that's for a single item, I can say.
All I enter, and now I get back to query sit where all their last names happen to be Smith.
I can also chain filters together.
So let's say I wanted to wear the last name is Smith, and they happened to be 40 years old.
I could do the same command as before, but now I can actually call Filter on top of that again.
So I can say filter, where let's say age is equal to 40.
And then I can call all.
And now I get the further refined query set.
And what's nice about this is this technically means you can assign this query set to a variable and
then call dot filter on that particular variable.
It's kind of up to you, depending on how you're actually organizing your views and templates and structures
with models on how exactly you're going to end up filtering things.
The other point I want to make here make clear this.
And now let's talk about operators within a query set in order to use this.
We have to import what's known as a queue object inside of Django as a quick note.
You should definitely check out the documentation on this.
It's kind of this weird beast, but once you get used to it, it allows you to have a lot more functionality
that is more skill based in order to filter out results.
So this is going to allow us to add an end or conditions and meaning I want both conditions to be true
or meaning.
I will either condition to be true in order to use.
This will have to say from.
Django, the D B, the model's import and then just capital Q.
OK.
And this is known as a Q object within Django.
So if you're ever wondering where the documentation is, you can just Google search Q objects within
Django.
And it's a Q object class.
Now again, this essentially allows you to use or and and operations later on work.
Discuss even more complex lookups with field lookup objects.
But right now we're just discussing or in and operations sort of come back here and let's imagine I
want people that are both have a last name of Smith and have an age greater than 30.
I could use to filter calls for this, but now that I have the queue operator, I can actually put this
within a single filter call.
So we're going to say patient the objects that and then we're going to say, is filter.
And inside this filter is where I can begin to use my Q statements.
So instead of stacking two filters, I'm going to say Q, and this is a function that takes in a condition
like something like name is equal to Smith.
Keep in mind, these conditions do need to be in equal condition.
Later on, we're going to discuss how to do something like less than or greater then.
So keep that in mind right now.
And if you want to keep stacking these, you have to use a pipe operator for or in an ampersand for.
In so case, you're not familiar if those again, it's this symbol for or sort of pipe operator and
an ampersand for end.
So let's use an ampersand for right now.
So I'm going to say ampersand typically shift seven or keyboard, and we're going to say, all right,
I'll want the last name equal to Smith.
And then we add another queue operator condition or queue object look up.
So we're going to say Q and let's say H.
There has to be equal to 40.
OK, so I want both those conditions to be true.
Notice we've technically already done something like this, but we did it with two filter calls.
Now I'm doing one filter call with the use of a queue object, and then we're going to say, grab me
everything that meets those conditions.
You run that and I get back.
Susan Smith, who's 40 years old, and Adam Smith, who's also 40 years old.
So that is the queue operator, which allows for complex lookups.
Keep this in mind because we're going to add queue operators with field lookups, which are even more
complex later on.
So so far, we've covered the ability to use get, which is for grabbing a single item.
We almost always use primary key with that because we expect a single item.
We've also learned how to filter based off conditions like, I want this attribute equal to this.
I want another attribute equal to that, and I can keep stacking those and then use all at the end to
grab everything.
Or I could also use this queue operator in order to put and or operations within my filter call.
So if I wanted to do last name is equal to Smith or ages 40, I could come back here and delete the
ampersand and put in a pipe operator.
That's essentially saying, give me all the entries that have the last name Smith or have an age of
40 years old.
Notice now I also get something like Carl Smith, who is 30 years old, but he also has the last name
Smith.
OK, so now we're going to do in the next lecture, expand our understanding of filtering with a very
powerful tool called field lookups.
I'll see you in the next lecture.