1
00:00:05,270 --> 00:00:09,610
Welcome back, everyone, to this lecture on reading and querying with your database.

2
00:00:10,830 --> 00:00:16,140
Each model you create comes with a manager that allows you to create what's known as a query set, which

3
00:00:16,140 --> 00:00:18,630
can then be used to retrieve entries from the database.

4
00:00:19,170 --> 00:00:24,870
Keep in mind that the query set is actually lazily evaluated, meaning that it doesn't hit the database

5
00:00:24,870 --> 00:00:27,540
until it's explicitly asked to grab the information.

6
00:00:27,960 --> 00:00:33,060
Which is nice because you don't want to ping your database for every extra filter that you add on to

7
00:00:33,060 --> 00:00:33,840
your query set.

8
00:00:34,260 --> 00:00:37,500
This will make more sense when we get into more advanced filtering techniques.

9
00:00:39,220 --> 00:00:45,010
So recall that previously we use something like my model, the objects in order to create or bulk creates

10
00:00:45,020 --> 00:00:45,550
entries.

11
00:00:46,000 --> 00:00:50,320
This is known as the Django model manager or just manager for short.

12
00:00:50,800 --> 00:00:55,480
This manager can then actually read the database through the use of different method calls in the same

13
00:00:55,480 --> 00:00:58,810
way it could create entries also through the use of method calls.

14
00:00:59,170 --> 00:01:04,930
There are the call and get method calls to actually retrieve and get the information from the database.

15
00:01:05,260 --> 00:01:09,820
And then you can also narrow down results with method calls like that filter and that exclude.

16
00:01:10,240 --> 00:01:15,610
And what's nice about this manager and the query set is just putting in that filter in that exclude

17
00:01:15,880 --> 00:01:17,900
do not explicitly call the database.

18
00:01:17,920 --> 00:01:22,510
It's not until you actually call for something like it, thought all or thought get that the database

19
00:01:22,510 --> 00:01:23,740
is hit with the request.

20
00:01:25,820 --> 00:01:30,770
So in this particular election, we're really just going to focus on the Dot all method, which allows

21
00:01:30,770 --> 00:01:33,440
us to grab all the entries in the database table.

22
00:01:34,010 --> 00:01:39,470
Typically, we won't want everything in a database table, so we'll need to filter our results, but

23
00:01:39,470 --> 00:01:43,280
we'll discuss filtering in more detail later on in the next lecture.

24
00:01:43,550 --> 00:01:48,350
For right now, we're really just exploring how do we grab everything from a table and what does that

25
00:01:48,350 --> 00:01:50,030
information actually look like?

26
00:01:51,580 --> 00:01:58,030
A general great resource on queries of examples can be found at Jingo Project com under the DB or database

27
00:01:58,030 --> 00:01:59,320
section under queries.

28
00:01:59,650 --> 00:02:02,470
That's really where we're referencing for the next series of lectures.

29
00:02:02,860 --> 00:02:08,020
For right now, we want to explore the very basics of reading and data that is all the entries in a

30
00:02:08,020 --> 00:02:09,910
tables entry points.

31
00:02:10,000 --> 00:02:15,250
And then what changes we can make to make the model actually have results that are more human readable.

32
00:02:15,730 --> 00:02:17,950
So let's head over to the code editor.

33
00:02:18,430 --> 00:02:21,370
OK, so here I am back inside Visual Studio code.

34
00:02:21,730 --> 00:02:23,950
I'm going to launch my shell again.

35
00:02:23,950 --> 00:02:27,790
Which remember is Python managed the pie shell.

36
00:02:28,510 --> 00:02:29,870
Go ahead and enter there.

37
00:02:29,920 --> 00:02:32,680
And again, because I'm using interactive python.

38
00:02:32,680 --> 00:02:38,350
If I python, because that's how I configured things in my computer, I get to see the input output.

39
00:02:38,350 --> 00:02:41,390
But yours may just be just a plain python shell.

40
00:02:41,410 --> 00:02:42,490
That's totally OK.

41
00:02:43,150 --> 00:02:43,480
So.

42
00:02:44,630 --> 00:02:50,750
Remember, I want to actually get access to the patient model, which means I need to import actually

43
00:02:50,750 --> 00:02:51,500
say from.

44
00:02:52,470 --> 00:02:56,550
And then office, the models, import patients.

45
00:02:57,570 --> 00:02:59,760
OK, so now I have patients available to us.

46
00:03:00,090 --> 00:03:04,230
Now we're going to do is we're actually going to query this database and see what happens.

47
00:03:04,710 --> 00:03:08,520
So the simplest way to do this is by simply saying patient objects.

48
00:03:08,820 --> 00:03:10,530
And this is the manager.

49
00:03:10,830 --> 00:03:16,650
The manager can then actually get a query set, and the simplest query is to simply just request give

50
00:03:16,650 --> 00:03:20,340
me all the objects or entries in this particular table or database.

51
00:03:20,670 --> 00:03:23,700
And this returns back a query set that looks a lot like a list.

52
00:03:24,300 --> 00:03:26,160
So you get back this query set.

53
00:03:26,430 --> 00:03:30,290
It is kind of like a list in the sense that you can actually index things from this.

54
00:03:30,300 --> 00:03:36,570
So for example, if you only wanted, let's say, the first item, you could put brackets there for

55
00:03:36,570 --> 00:03:40,490
zero and then it would just return back the first patient.

56
00:03:40,500 --> 00:03:44,580
So notice it does have some sort of primary I.D. or primary care.

57
00:03:44,810 --> 00:03:46,770
One, two, three or four or five, et cetera.

58
00:03:47,100 --> 00:03:49,170
But you're probably wondering, well, what's the deal?

59
00:03:49,170 --> 00:03:52,350
Why is there more information about the first name, last name or age?

60
00:03:52,770 --> 00:03:56,250
Well, recall appear in our model.

61
00:03:56,610 --> 00:04:00,330
There is no human readable screen representation of this class.

62
00:04:00,330 --> 00:04:06,540
And in fact, you would get this for any object you attempted to print out in Python that didn't have

63
00:04:06,570 --> 00:04:08,280
a string method attached to it.

64
00:04:08,610 --> 00:04:11,490
So let's add that in so that it's more readable for us.

65
00:04:12,000 --> 00:04:18,839
We're going to say the E.F. underscore underscore S.T.A.R. underscore underscore percent self.

66
00:04:19,560 --> 00:04:24,660
And now we're just going to return whatever the string representation we want for this particular model

67
00:04:24,660 --> 00:04:25,350
object this.

68
00:04:25,710 --> 00:04:28,200
So I'm going to use string literal formatting here.

69
00:04:28,710 --> 00:04:35,130
And let's say that I want to report back the last name, first comma, first name again.

70
00:04:35,130 --> 00:04:35,910
It's kind of up to you.

71
00:04:35,940 --> 00:04:37,770
So we'll say solved that last name.

72
00:04:39,060 --> 00:04:44,400
And then I'm also going to say soft first name, and let's put a comma here.

73
00:04:45,350 --> 00:04:52,910
And let's say is and then let's report their age, self-taught, eight years old.

74
00:04:53,660 --> 00:04:58,420
OK, so this is kind of a long string representation, but hopefully you get the idea.

75
00:04:58,430 --> 00:05:00,710
You can obviously edit this to whatever's most convenient.

76
00:05:00,980 --> 00:05:05,240
Not sure if it's really necessary to say their age when we report back patients, but you get the idea.

77
00:05:05,600 --> 00:05:12,050
So I'm going to save that change in models that pay in order to register these changes for the ST method.

78
00:05:12,320 --> 00:05:15,320
I do need to restart my shell again.

79
00:05:15,560 --> 00:05:19,550
Otherwise it hasn't actually registered the changes to the patient class you imported.

80
00:05:20,000 --> 00:05:25,310
So what I'm going to say is, again, Python, after you've quit out of the other shells, remember

81
00:05:25,430 --> 00:05:29,240
to quit out of the shell is simply quit open close parentheses.

82
00:05:29,720 --> 00:05:33,680
But now I'm going to restart it with Python managed the pie shell.

83
00:05:34,830 --> 00:05:35,400
There we go.

84
00:05:35,430 --> 00:05:36,990
And so let's import again from.

85
00:05:39,540 --> 00:05:40,590
Office, not models.

86
00:05:42,490 --> 00:05:43,360
In poor patient.

87
00:05:44,330 --> 00:05:47,150
And then we're going to say again, patients, the objects.

88
00:05:48,170 --> 00:05:53,990
All enter and not your see, your query is a lot different, says Smith Khalil's, 30 years old.

89
00:05:54,320 --> 00:06:00,200
And again, you could just grab the very first one if you wanted to see something like you have an instance

90
00:06:00,410 --> 00:06:02,990
from the patient table or a patient model.

91
00:06:03,230 --> 00:06:05,240
And then Smith Carl is three years old.

92
00:06:05,660 --> 00:06:13,460
OK, so that is how we can grab everything from a database table or just grab particular instances as

93
00:06:13,460 --> 00:06:17,810
well as slicing, so you could also slice with index notation.

94
00:06:18,200 --> 00:06:21,740
So, for example, I can go zero all the way up to, but not including index two.

95
00:06:22,040 --> 00:06:24,120
And I see a query set with two patients.

96
00:06:24,980 --> 00:06:28,970
Typically, we don't want to just grab everything and then index off of that.

97
00:06:29,150 --> 00:06:32,900
Instead, we want to filter and then get particular entries.

98
00:06:33,140 --> 00:06:35,120
So let's talk about that in the next lecture.

