1
00:00:05,190 --> 00:00:10,740
Welcome everyone to this continued discussion of filtering query results in this lecture will focus

2
00:00:10,740 --> 00:00:12,660
on a filter and get methods.

3
00:00:13,820 --> 00:00:19,310
The get operation or method allows us to grab a single item from the model table.

4
00:00:19,670 --> 00:00:24,020
Now this is typically reserved for something where you're pretty sure there's only one single unique

5
00:00:24,020 --> 00:00:24,500
entry.

6
00:00:24,710 --> 00:00:26,690
Otherwise, get actually returns an error.

7
00:00:27,110 --> 00:00:32,509
For example, the default primary key that is automatically created by jingo, such as Piqué, is equal

8
00:00:32,509 --> 00:00:38,180
to one, two, three or any other number, which has a unique index position row in that table.

9
00:00:38,240 --> 00:00:40,430
That's typically what you see get used for.

10
00:00:41,700 --> 00:00:46,950
If we want to further filter our results rather than just grab all or get a single item, we can use

11
00:00:46,950 --> 00:00:51,780
the filter method to narrow down based on conditions and the filter methods can actually be chained

12
00:00:51,780 --> 00:00:52,340
together.

13
00:00:54,010 --> 00:00:59,620
Django also provides operators for query sets, which allows us to actually directly use logic like

14
00:00:59,620 --> 00:01:03,130
an and operator or operator within a single filter call.

15
00:01:03,670 --> 00:01:09,370
So these operators actually need to be imported from Django DB models from the queue function, and

16
00:01:09,370 --> 00:01:10,900
we'll show you how to do that later on.

17
00:01:11,910 --> 00:01:17,430
So what we're going to do is explore the following topics getting a single entry from our table with

18
00:01:17,430 --> 00:01:23,730
the get method filtering down to get maybe a set that we're interested in of results and then how to

19
00:01:23,730 --> 00:01:25,830
use operators in conjunction with filters.

20
00:01:26,130 --> 00:01:27,600
Let's head back to our code editor.

21
00:01:28,900 --> 00:01:35,210
OK, so here I am inside Visual Studio code recall that our model technically has three fields or attributes.

22
00:01:35,230 --> 00:01:41,260
The first name, last name and h, but also remember that when we actually ran the initial migration,

23
00:01:41,650 --> 00:01:48,700
jingo automatically creates an I.D. for this model, and the I.D. is a big auto field.

24
00:01:49,240 --> 00:01:55,720
Auto stands for the fact that it's going to automatically increment that number each time you actually

25
00:01:55,720 --> 00:01:57,010
put in a new row.

26
00:01:57,040 --> 00:02:00,310
So it's going to be and then an +1 and +2 and so on.

27
00:02:00,310 --> 00:02:05,710
So it starts at the number one, then two, the three, then four, etc. Big just means it allows it

28
00:02:05,710 --> 00:02:11,140
to be a very large integer because jingo technically doesn't know how many entries are going to put.

29
00:02:11,380 --> 00:02:14,350
So it allows us to be a very large integer number.

30
00:02:15,100 --> 00:02:21,040
That means every time you actually put in a new data entry row into your table, there's going to be

31
00:02:21,070 --> 00:02:23,950
a primary key or ID associated with it.

32
00:02:24,250 --> 00:02:31,180
And the primary key is equal to true is what's associating the fact that this ID column or index is

33
00:02:31,180 --> 00:02:32,950
actually the primary key as well.

34
00:02:33,430 --> 00:02:39,040
This is super useful because now you have a unique identifier for each entry in your data table.

35
00:02:39,050 --> 00:02:44,410
That means even if you have a patient with the same first name and same last name and same age as someone

36
00:02:44,410 --> 00:02:50,050
else like Mike Smith age 30 and you happen to have two patients with that, you can uniquely identify

37
00:02:50,050 --> 00:02:51,580
them by their I.D..

38
00:02:52,030 --> 00:02:57,820
You can think about this in the same way as your driver's license has a unique ID number or your medical

39
00:02:57,820 --> 00:03:00,070
insurance card or your Citizen's ID.

40
00:03:00,490 --> 00:03:05,410
All have unique ID numbers that we don't get confused if you happen to share the same first name, last

41
00:03:05,410 --> 00:03:07,330
name and birthday as someone else.

42
00:03:07,810 --> 00:03:14,680
OK, so the fact that we know I.D. is a unique identifier then allows us to use get.

43
00:03:14,980 --> 00:03:24,220
So we're going to do here is, say, patients that objects that get and then I can say piqué or primary

44
00:03:24,220 --> 00:03:27,100
key and then set the sequel to something like one.

45
00:03:27,670 --> 00:03:33,510
Keep in mind in sequel, the indexing starts at one, not zero, like in Python.

46
00:03:33,520 --> 00:03:39,850
So if I want the very first item that I ever entered, I would say Piqué is equal to one, and then

47
00:03:39,850 --> 00:03:41,170
I would get back my patient.

48
00:03:41,170 --> 00:03:43,660
Smith or Carl Smith is three years old.

49
00:03:43,990 --> 00:03:47,800
Remember, Carl Smith was the very first patient I put into this table.

50
00:03:48,520 --> 00:03:55,930
You should also note that it is only designed to grab the single item that can be retrieved, and it's

51
00:03:55,930 --> 00:04:01,090
actually going to report back an error if you try to grab more than one item, and we'll show more on

52
00:04:01,090 --> 00:04:01,650
that later.

53
00:04:01,660 --> 00:04:06,720
For example, maybe I say patient objects doc get in.

54
00:04:06,850 --> 00:04:14,110
Imagine I tried to put some sort of filter in this like first name equal to Smith because I know there's

55
00:04:14,110 --> 00:04:15,010
more than one there.

56
00:04:15,280 --> 00:04:19,209
If I try to do something like this, this is going to give you an error and it's going to say, Hey,

57
00:04:19,209 --> 00:04:20,140
that doesn't exist.

58
00:04:20,140 --> 00:04:23,550
And the fact that there is no first name was Smith.

59
00:04:23,560 --> 00:04:24,720
That's one possible error.

60
00:04:24,760 --> 00:04:26,050
This patient just doesn't exist.

61
00:04:26,410 --> 00:04:29,440
Another possible error is if I say last name Smith.

62
00:04:30,940 --> 00:04:35,680
So note here, I'm saying last name Smith, which I know is actually something in my database.

63
00:04:36,280 --> 00:04:37,990
Then I get back another error.

64
00:04:38,080 --> 00:04:39,550
Multiple objects return.

65
00:04:39,670 --> 00:04:40,540
And this is key.

66
00:04:40,750 --> 00:04:47,620
The get method can only return a single object, and that's specifically designed to make sure when

67
00:04:47,620 --> 00:04:53,800
you're using get, you should be using it in a place where you only expect a single patient or a single

68
00:04:53,800 --> 00:04:56,310
data row to be returned, something like primary key.

69
00:04:56,800 --> 00:05:02,350
And it actually has a really nice error Message says, Hey, this returned more than one patient and

70
00:05:02,350 --> 00:05:03,640
it actually says an irritant.

71
00:05:03,670 --> 00:05:04,030
Three.

72
00:05:04,450 --> 00:05:09,520
So this is basically telling you you should probably be using all again with some sort of filter attached

73
00:05:09,520 --> 00:05:09,790
to it.

74
00:05:10,300 --> 00:05:15,590
So let's talk about filters because you are not going to typically be running a filter like this.

75
00:05:15,610 --> 00:05:17,860
Instead, you actually do a filter call.

76
00:05:18,070 --> 00:05:19,120
So we do the following.

77
00:05:19,450 --> 00:05:26,290
Let me clear this I'm going to say patient the objects and then I call a filter.

78
00:05:27,010 --> 00:05:33,430
And inside this filter is where I can begin to do things like Boolean statements, so I can say something

79
00:05:33,430 --> 00:05:36,100
like, OK, filter, where the last name?

80
00:05:37,200 --> 00:05:38,340
Is equal to Smith.

81
00:05:38,370 --> 00:05:40,050
Notice I'm doing all lowercase Smith here.

82
00:05:41,160 --> 00:05:44,670
And you're going to have match access to whatever data entries you put in as well.

83
00:05:45,090 --> 00:05:50,070
So then instead of saying get because that's for a single item, I can say.

84
00:05:51,000 --> 00:05:56,550
All I enter, and now I get back to query sit where all their last names happen to be Smith.

85
00:05:57,480 --> 00:05:59,820
I can also chain filters together.

86
00:06:00,180 --> 00:06:04,320
So let's say I wanted to wear the last name is Smith, and they happened to be 40 years old.

87
00:06:04,680 --> 00:06:09,720
I could do the same command as before, but now I can actually call Filter on top of that again.

88
00:06:10,200 --> 00:06:16,040
So I can say filter, where let's say age is equal to 40.

89
00:06:16,740 --> 00:06:17,880
And then I can call all.

90
00:06:19,480 --> 00:06:22,840
And now I get the further refined query set.

91
00:06:23,140 --> 00:06:28,540
And what's nice about this is this technically means you can assign this query set to a variable and

92
00:06:28,540 --> 00:06:31,960
then call dot filter on that particular variable.

93
00:06:32,290 --> 00:06:36,460
It's kind of up to you, depending on how you're actually organizing your views and templates and structures

94
00:06:36,460 --> 00:06:39,580
with models on how exactly you're going to end up filtering things.

95
00:06:40,030 --> 00:06:42,370
The other point I want to make here make clear this.

96
00:06:44,250 --> 00:06:49,560
And now let's talk about operators within a query set in order to use this.

97
00:06:49,860 --> 00:06:55,720
We have to import what's known as a queue object inside of Django as a quick note.

98
00:06:55,740 --> 00:06:57,930
You should definitely check out the documentation on this.

99
00:06:57,960 --> 00:07:03,690
It's kind of this weird beast, but once you get used to it, it allows you to have a lot more functionality

100
00:07:03,960 --> 00:07:07,200
that is more skill based in order to filter out results.

101
00:07:07,530 --> 00:07:13,950
So this is going to allow us to add an end or conditions and meaning I want both conditions to be true

102
00:07:14,100 --> 00:07:14,820
or meaning.

103
00:07:14,820 --> 00:07:17,670
I will either condition to be true in order to use.

104
00:07:17,670 --> 00:07:19,350
This will have to say from.

105
00:07:20,500 --> 00:07:25,930
Django, the D B, the model's import and then just capital Q.

106
00:07:26,840 --> 00:07:27,120
OK.

107
00:07:27,140 --> 00:07:29,910
And this is known as a Q object within Django.

108
00:07:29,930 --> 00:07:34,580
So if you're ever wondering where the documentation is, you can just Google search Q objects within

109
00:07:34,580 --> 00:07:35,030
Django.

110
00:07:35,240 --> 00:07:37,250
And it's a Q object class.

111
00:07:37,370 --> 00:07:42,940
Now again, this essentially allows you to use or and and operations later on work.

112
00:07:42,980 --> 00:07:46,570
Discuss even more complex lookups with field lookup objects.

113
00:07:46,580 --> 00:07:53,690
But right now we're just discussing or in and operations sort of come back here and let's imagine I

114
00:07:53,690 --> 00:08:00,950
want people that are both have a last name of Smith and have an age greater than 30.

115
00:08:01,010 --> 00:08:06,890
I could use to filter calls for this, but now that I have the queue operator, I can actually put this

116
00:08:06,890 --> 00:08:08,750
within a single filter call.

117
00:08:09,260 --> 00:08:15,530
So we're going to say patient the objects that and then we're going to say, is filter.

118
00:08:16,250 --> 00:08:20,200
And inside this filter is where I can begin to use my Q statements.

119
00:08:20,210 --> 00:08:26,660
So instead of stacking two filters, I'm going to say Q, and this is a function that takes in a condition

120
00:08:26,660 --> 00:08:30,230
like something like name is equal to Smith.

121
00:08:30,680 --> 00:08:34,370
Keep in mind, these conditions do need to be in equal condition.

122
00:08:34,669 --> 00:08:38,059
Later on, we're going to discuss how to do something like less than or greater then.

123
00:08:38,070 --> 00:08:39,620
So keep that in mind right now.

124
00:08:40,190 --> 00:08:45,860
And if you want to keep stacking these, you have to use a pipe operator for or in an ampersand for.

125
00:08:45,860 --> 00:08:51,970
In so case, you're not familiar if those again, it's this symbol for or sort of pipe operator and

126
00:08:51,980 --> 00:08:53,570
an ampersand for end.

127
00:08:53,750 --> 00:08:55,640
So let's use an ampersand for right now.

128
00:08:56,150 --> 00:09:01,100
So I'm going to say ampersand typically shift seven or keyboard, and we're going to say, all right,

129
00:09:01,100 --> 00:09:02,900
I'll want the last name equal to Smith.

130
00:09:02,900 --> 00:09:07,130
And then we add another queue operator condition or queue object look up.

131
00:09:07,700 --> 00:09:10,520
So we're going to say Q and let's say H.

132
00:09:11,520 --> 00:09:13,950
There has to be equal to 40.

133
00:09:16,800 --> 00:09:18,930
OK, so I want both those conditions to be true.

134
00:09:19,290 --> 00:09:23,190
Notice we've technically already done something like this, but we did it with two filter calls.

135
00:09:23,610 --> 00:09:29,460
Now I'm doing one filter call with the use of a queue object, and then we're going to say, grab me

136
00:09:29,880 --> 00:09:31,570
everything that meets those conditions.

137
00:09:31,590 --> 00:09:33,000
You run that and I get back.

138
00:09:33,270 --> 00:09:36,720
Susan Smith, who's 40 years old, and Adam Smith, who's also 40 years old.

139
00:09:37,170 --> 00:09:41,610
So that is the queue operator, which allows for complex lookups.

140
00:09:41,940 --> 00:09:47,400
Keep this in mind because we're going to add queue operators with field lookups, which are even more

141
00:09:47,400 --> 00:09:48,720
complex later on.

142
00:09:49,140 --> 00:09:53,790
So so far, we've covered the ability to use get, which is for grabbing a single item.

143
00:09:54,150 --> 00:09:57,570
We almost always use primary key with that because we expect a single item.

144
00:09:57,960 --> 00:10:03,290
We've also learned how to filter based off conditions like, I want this attribute equal to this.

145
00:10:03,300 --> 00:10:07,950
I want another attribute equal to that, and I can keep stacking those and then use all at the end to

146
00:10:07,950 --> 00:10:08,780
grab everything.

147
00:10:09,210 --> 00:10:16,680
Or I could also use this queue operator in order to put and or operations within my filter call.

148
00:10:17,040 --> 00:10:23,580
So if I wanted to do last name is equal to Smith or ages 40, I could come back here and delete the

149
00:10:23,580 --> 00:10:25,800
ampersand and put in a pipe operator.

150
00:10:26,190 --> 00:10:30,780
That's essentially saying, give me all the entries that have the last name Smith or have an age of

151
00:10:30,780 --> 00:10:31,530
40 years old.

152
00:10:31,950 --> 00:10:37,200
Notice now I also get something like Carl Smith, who is 30 years old, but he also has the last name

153
00:10:37,200 --> 00:10:37,560
Smith.

154
00:10:38,100 --> 00:10:45,690
OK, so now we're going to do in the next lecture, expand our understanding of filtering with a very

155
00:10:45,690 --> 00:10:48,270
powerful tool called field lookups.

156
00:10:48,480 --> 00:10:49,440
I'll see you in the next lecture.

