1
00:00:05,490 --> 00:00:09,450
Welcome everyone to this series of lectures on discussing data interaction.

2
00:00:09,780 --> 00:00:16,290
As we previously mentioned, we need to be able to create or insert read updates and delete information

3
00:00:16,290 --> 00:00:17,340
from our database.

4
00:00:17,790 --> 00:00:21,330
Let's begin by discussing creation and insertion into the database.

5
00:00:22,610 --> 00:00:26,600
Inserting new data into a school table is super easy with changing models.

6
00:00:27,030 --> 00:00:32,930
Recall, since the models are represented by a Python class, we can easily create a new instance of

7
00:00:32,930 --> 00:00:39,170
the class object in Python defined the attributes and then call the dot safe method on that class to

8
00:00:39,170 --> 00:00:41,720
create an insert call to the school database.

9
00:00:43,170 --> 00:00:48,870
Alternatively, you can use the built in the objects, the create method to both creates and save the

10
00:00:48,870 --> 00:00:54,120
new data entry in a single line that saves you the two step process of having to create and then save

11
00:00:54,120 --> 00:00:54,660
the changes.

12
00:00:54,870 --> 00:01:00,090
You can just call the objects to create, and they'll both create and push that save into the actual

13
00:01:00,090 --> 00:01:00,680
database.

14
00:01:02,160 --> 00:01:06,990
In instances where you want to create multiple new data entries in bulk in a single call, you can use

15
00:01:06,990 --> 00:01:12,180
the objects that bulk underscore create method to pass any list of newly created objects.

16
00:01:13,050 --> 00:01:16,800
So let's explore these three methods of creating new data entries of a model.

17
00:01:17,130 --> 00:01:21,300
The first of the simplest where we actually separate out into two steps creating the object and then

18
00:01:21,300 --> 00:01:26,850
saving it to the database, then we do it in one step with objects to create, and then we can do multiple

19
00:01:26,850 --> 00:01:27,920
at a time with objects.

20
00:01:27,920 --> 00:01:29,100
Stop, bulk, create.

21
00:01:29,490 --> 00:01:32,910
Keep in mind, we haven't taught you how to read in data from a database.

22
00:01:33,180 --> 00:01:34,690
Still, just have to take my word for it.

23
00:01:34,710 --> 00:01:39,630
For now that these entries are actually being inserted into the database, in the next lecture, we'll

24
00:01:39,630 --> 00:01:43,230
discuss reading information in order to confirm the entries we do here.

25
00:01:43,590 --> 00:01:45,270
OK, let's head over to our code editor.

26
00:01:46,110 --> 00:01:51,330
All right, so here I am inside Visual Studio code and we've set up our models and we've already ran

27
00:01:51,330 --> 00:01:52,320
those migrations.

28
00:01:52,860 --> 00:01:57,800
Now, how can I actually explore and play around with the model and the database?

29
00:01:57,810 --> 00:02:04,140
One way would be to build an entire website and have forms on the front end and then run those and check

30
00:02:04,140 --> 00:02:07,380
through admin on the backend to see if those changes were actually updated.

31
00:02:07,800 --> 00:02:12,750
Now that's a lot of work, just to begin to understand the way the model calls.

32
00:02:12,750 --> 00:02:16,950
That is, the method calls on a model object actually operate and interact with the database.

33
00:02:17,340 --> 00:02:23,670
So instead, when you want to just play around with some of the files already created with Django Django

34
00:02:23,670 --> 00:02:30,150
inside of Managed API actually offers you a shell command, and that shell command allows you to then

35
00:02:30,150 --> 00:02:36,060
interact with the Python files without having to go through the entire process of building out your

36
00:02:36,060 --> 00:02:38,310
website, which keep in mind, we will eventually do.

37
00:02:38,550 --> 00:02:42,900
Clearly, we're not going to be actually editing these models at the terminal command line, but for

38
00:02:42,900 --> 00:02:47,670
right now, just to get an idea of how different methods work, interact and operate, we'll be doing

39
00:02:47,670 --> 00:02:48,630
this at the terminal.

40
00:02:49,140 --> 00:02:54,870
So in order to run your actual shell, what you do is at my site.

41
00:02:55,980 --> 00:03:01,680
We're going to call again managed pie and then one of its different sub commands in the same way it

42
00:03:01,680 --> 00:03:06,120
has a start up Sub Command is going to be the shell command.

43
00:03:06,690 --> 00:03:11,820
Go ahead and run python, manage that pie shell and you'll see something that looks like this essentially

44
00:03:12,000 --> 00:03:14,580
a connection between python and shell.

45
00:03:15,090 --> 00:03:18,430
Now, keep in mind, you may not actually be using a python.

46
00:03:18,840 --> 00:03:24,180
I have an enhanced, interactive python that I've configured of this terminal for demonstration purposes

47
00:03:24,180 --> 00:03:24,600
only.

48
00:03:24,870 --> 00:03:29,520
What's nice about this is it performs a little bit of syntax highlighting and also directly reports

49
00:03:29,520 --> 00:03:30,530
what the input was.

50
00:03:30,540 --> 00:03:32,160
And then later on, you'll see what the output was.

51
00:03:32,550 --> 00:03:35,100
So I can say something like Prince, hello.

52
00:03:35,880 --> 00:03:39,750
And you can see that syntax highlighting enter and then I get to see the output.

53
00:03:40,020 --> 00:03:40,410
Hello.

54
00:03:40,710 --> 00:03:43,530
It's very similar to the way Jupyter Notebook operates.

55
00:03:43,530 --> 00:03:46,550
If you have some familiarity of that, do not worry at all.

56
00:03:46,560 --> 00:03:50,490
If you just see a normal Python interpreter, that's totally OK.

57
00:03:50,730 --> 00:03:52,740
All the code is going to work the same as well.

58
00:03:53,280 --> 00:03:54,930
So let's go ahead and continue.

59
00:03:55,410 --> 00:03:57,780
First, I want to be able to use this patient model.

60
00:03:58,170 --> 00:04:02,340
So what I'm going to say is the following I'm going to say.

61
00:04:03,280 --> 00:04:03,760
From.

62
00:04:05,510 --> 00:04:16,070
Office dot models import patient because inside this office folder is in its PDF file and models that

63
00:04:16,070 --> 00:04:16,370
pie.

64
00:04:16,760 --> 00:04:19,160
So I'm going to say from office, not models, import patients.

65
00:04:19,610 --> 00:04:24,470
I read that and now I should be able to actually create a patient's object.

66
00:04:24,620 --> 00:04:26,630
So let's do that, let's say.

67
00:04:28,260 --> 00:04:30,840
In fact, let's just call this object, Carl.

68
00:04:31,760 --> 00:04:33,650
And we're going to create a new patient called Carl.

69
00:04:34,720 --> 00:04:37,660
And we'll see patients and then this war after the find the first name.

70
00:04:38,230 --> 00:04:41,680
So say first name is going to be equal to.

71
00:04:43,470 --> 00:04:44,460
Let's say, Karl.

72
00:04:46,320 --> 00:04:48,450
Last name is going to be equal to.

73
00:04:48,990 --> 00:04:50,940
And then I'm going to say Smith.

74
00:04:52,360 --> 00:04:54,760
It's probably capitalize those, but not a big deal right now.

75
00:04:55,510 --> 00:04:58,960
Only constraint is to make sure they're only 30 characters and then the age.

76
00:04:59,890 --> 00:05:03,970
Remember, the age has to be an integer, so we're gonna say age is, let's say, 30.

77
00:05:06,050 --> 00:05:13,070
OK, so that's technically number one, I have this Python object that exists, but Carl himself first

78
00:05:13,070 --> 00:05:14,960
named Carl Larson Smith age 30.

79
00:05:15,380 --> 00:05:22,250
This actual data wrote instance is not yet inserted into my SQL database, so that means I can still

80
00:05:22,250 --> 00:05:24,290
do any sort of checks I want with Carl.

81
00:05:24,560 --> 00:05:29,290
So, for example, I could say, Carl, check your age and it reports back.

82
00:05:29,300 --> 00:05:30,080
Or I could check.

83
00:05:30,740 --> 00:05:32,240
I could do something like Carl age.

84
00:05:32,240 --> 00:05:33,740
Is it less than 20?

85
00:05:34,310 --> 00:05:35,540
And it would give me false.

86
00:05:35,660 --> 00:05:41,210
Hopefully, it gives you the idea that you can do still a lot of functionality on the Python object

87
00:05:41,300 --> 00:05:46,310
without having to actually commit it to the database so you can run any sort of checks or validations

88
00:05:46,520 --> 00:05:48,170
yourself using Python code.

89
00:05:48,560 --> 00:05:51,470
Later on, we'll talk about validators they can actually build into the model.

90
00:05:51,740 --> 00:05:57,680
But keep in mind, you can do all sorts of checks and different interactions with this Python object

91
00:05:57,920 --> 00:05:59,660
before you committed to the database.

92
00:06:00,110 --> 00:06:05,090
Now, let's say Karl looks like the ages, OK, and we are ready to end up pushing this to our actual

93
00:06:05,090 --> 00:06:05,600
database.

94
00:06:06,020 --> 00:06:12,310
Then what we can say here is Karl, and you simply call that safe enter.

95
00:06:12,380 --> 00:06:19,340
And this itself just ran the insert command to now be part of the database here and SQLite three.

96
00:06:19,820 --> 00:06:23,430
You may be wondering where does this that safe method call actually come from?

97
00:06:23,750 --> 00:06:26,870
It comes from the inherited models that model class.

98
00:06:27,860 --> 00:06:34,020
OK, so this is the simplest way to actually create a new entry into our database.

99
00:06:34,040 --> 00:06:39,320
You create an instance of that actual model class like patient.

100
00:06:39,650 --> 00:06:42,200
You could grab attributes from it and check it out.

101
00:06:42,500 --> 00:06:44,990
But at the end of day, you were going to then save it.

102
00:06:45,080 --> 00:06:48,290
So again, it's two steps creating it and then saving it.

103
00:06:48,920 --> 00:06:54,110
Now, let's imagine you already know that you want to create Karl with first name, last name and age

104
00:06:54,110 --> 00:06:55,160
or some other patient.

105
00:06:55,450 --> 00:06:57,950
Idon't want to have to separate this out into two calls here.

106
00:06:57,960 --> 00:07:00,560
You're just going to accept it right off the bat as correct.

107
00:07:00,980 --> 00:07:03,860
Well, what you can do there is the following you can say.

108
00:07:04,900 --> 00:07:10,800
Patient and then you're going to call dot objects dot create.

109
00:07:11,620 --> 00:07:18,070
And this is a shorthand for then accepting the attribute calls and calling them, say automatically.

110
00:07:18,550 --> 00:07:21,190
So we say patients to objects that create.

111
00:07:21,640 --> 00:07:23,650
And here you can then say first name.

112
00:07:24,580 --> 00:07:25,660
Let's say she, Susan.

113
00:07:27,410 --> 00:07:28,070
Last name.

114
00:07:28,760 --> 00:07:32,390
I'm also going to say Smith, and let's make her age.

115
00:07:33,880 --> 00:07:34,270
Forty.

116
00:07:35,450 --> 00:07:35,780
OK.

117
00:07:36,260 --> 00:07:43,550
So I run this command and we can see a new patient has been created and this patient, Susan Smith,

118
00:07:43,760 --> 00:07:50,450
has also now been saved to our actual database, so she exists inside this DB sequel like three.

119
00:07:50,810 --> 00:07:53,780
And let's do just one more patient using objects to create.

120
00:07:55,350 --> 00:07:59,850
And I just cleared this, you can also clear this with seals in Python.

121
00:08:00,390 --> 00:08:03,330
But don't worry about that right now if you're running a normal python.

122
00:08:03,780 --> 00:08:08,160
And what we're going to do here is the following I'm going to say again patients remember I already

123
00:08:08,160 --> 00:08:11,910
imported this and say objects that create.

124
00:08:12,360 --> 00:08:18,000
And then what I'm going to do here again is, say, first name is equal to and let's just have this

125
00:08:18,000 --> 00:08:18,870
one be called me me.

126
00:08:20,120 --> 00:08:27,800
Last name, we're going to have this one be called, say, bullies, and let's have the HP 36.

127
00:08:28,640 --> 00:08:29,920
OK, go ahead and hit.

128
00:08:29,960 --> 00:08:35,289
Enter here and you'll notice that the number actually reflects the index position.

129
00:08:35,299 --> 00:08:37,100
So this is technically the third patient.

130
00:08:37,100 --> 00:08:38,330
So we have index.

131
00:08:38,330 --> 00:08:43,400
One was the very first patient we created with creating the object, then saving it to a Susan Smith.

132
00:08:43,400 --> 00:08:45,530
And then three again, here is Mimi Bolus.

133
00:08:45,860 --> 00:08:46,160
All right.

134
00:08:46,580 --> 00:08:51,170
So the final thing I want to show you is the ability to create multiple patients at once.

135
00:08:51,620 --> 00:08:54,950
So let me clear this and expand a little bit here.

136
00:08:55,730 --> 00:09:01,010
Since this is going to take multiple lines, but sometimes you want to bulk create, how does that actually

137
00:09:01,010 --> 00:09:01,310
work?

138
00:09:01,640 --> 00:09:02,990
You're going to use the following.

139
00:09:04,320 --> 00:09:09,780
We'll be using the bulk creates method before we actually run it, though, I do want to point out the

140
00:09:09,780 --> 00:09:15,420
documentation because this is such a powerful tool to just do a bunch of entries at once.

141
00:09:15,900 --> 00:09:19,050
You may want to take into account the caveats here.

142
00:09:19,080 --> 00:09:24,900
So there's a couple of caveats of different types of databases or relationships between tables that

143
00:09:24,900 --> 00:09:26,640
this won't technically work for.

144
00:09:26,940 --> 00:09:29,580
So, for example, it doesn't work with many too many relationships.

145
00:09:29,940 --> 00:09:34,290
We'll explain more on that later when we actually create tables that have relationships between each

146
00:09:34,290 --> 00:09:34,560
other.

147
00:09:34,830 --> 00:09:38,610
But just keep in mind, there are some caveats here to using this tool.

148
00:09:38,910 --> 00:09:45,150
For most basic use cases, though, it should be fine to just pass any list of your new objects themselves.

149
00:09:45,390 --> 00:09:50,540
So we're going to do here is I'm going to create a separate list of new patients and then use entry

150
00:09:50,570 --> 00:09:54,090
the objects that bull create to then just quickly insert that list.

151
00:09:54,390 --> 00:09:58,530
And keep in mind, it's generally only one query, no matter how many objects there are.

152
00:09:58,800 --> 00:10:04,380
You could technically use the batch size argument to try to fix this if it was a huge amount of entries,

153
00:10:04,380 --> 00:10:06,840
but for most cases, this should be totally fine.

154
00:10:07,590 --> 00:10:09,810
So we're going to do here or show you how to do it with patients.

155
00:10:10,290 --> 00:10:12,330
So let's make a list of patients.

156
00:10:12,330 --> 00:10:13,890
I'm going to say my list.

157
00:10:14,930 --> 00:10:20,360
Is equal to that, let's create some patients here, so we'll say patient, remember, I already imported

158
00:10:20,360 --> 00:10:20,670
this.

159
00:10:20,780 --> 00:10:23,090
So the first name of this patient?

160
00:10:24,410 --> 00:10:29,240
Is going to be, let's say, Adam, and then let's say, last name.

161
00:10:30,250 --> 00:10:31,090
Is Smith?

162
00:10:32,560 --> 00:10:35,830
And then we'll say age is 40.

163
00:10:36,730 --> 00:10:38,210
So that's our first patient.

164
00:10:38,230 --> 00:10:40,270
And let's add one more patient to this list.

165
00:10:40,930 --> 00:10:42,070
We're going to say patients.

166
00:10:43,140 --> 00:10:45,000
And let's say the first name here.

167
00:10:45,660 --> 00:10:49,740
Well, I guess since we're talking about Adam Smith or also put in his buddy Karl Marx here.

168
00:10:49,780 --> 00:10:57,060
Not really, but you get the idea and then we'll say last name Marx, and then we'll say age is also

169
00:10:57,060 --> 00:10:57,360
40.

170
00:10:58,710 --> 00:11:00,750
OK, and then remember to close off that list.

171
00:11:00,930 --> 00:11:05,220
So I just created a new list, my list, and it has two patients in that list.

172
00:11:05,600 --> 00:11:06,900
Adam Smith and Karl Marx.

173
00:11:07,260 --> 00:11:09,480
Maybe they can duke it out there inside the database.

174
00:11:09,900 --> 00:11:12,540
And then what we're going to do is we're going to call entry.

175
00:11:12,540 --> 00:11:15,390
The objects are actually patient that objects and then bulk create.

176
00:11:15,870 --> 00:11:18,420
So I'm going to call patients that.

177
00:11:19,360 --> 00:11:22,060
Objects and similar to I can call create.

178
00:11:22,570 --> 00:11:27,730
Now I'm going to call bulk create and then here can pass in my list.

179
00:11:28,950 --> 00:11:35,610
Run that, and you can see it created those patient objects, and it reports them back essentially that

180
00:11:35,610 --> 00:11:36,570
they got inserted.

181
00:11:37,450 --> 00:11:43,240
Next, we have to actually see how this worked, so we would like to be able to read the results that

182
00:11:43,240 --> 00:11:47,020
are in the database, not just create and say everything's OK.

183
00:11:47,320 --> 00:11:50,030
And in fact, you notice that on some of these doesn't actually tell you the idea.

184
00:11:50,380 --> 00:11:50,830
None.

185
00:11:51,190 --> 00:11:56,500
So we want to explore all of that and more in the next lecture where we talk about reading information

186
00:11:56,500 --> 00:11:57,310
from a database.

187
00:11:57,610 --> 00:12:02,680
But as a review, remember, you can actually create a new object, then call save on it.

188
00:12:03,250 --> 00:12:08,500
You can say objects that create and then actually create a single object and save it in one command.

189
00:12:08,800 --> 00:12:11,620
Or if you want to quickly put in a lot, you can say bulk underscore.

190
00:12:11,620 --> 00:12:12,040
Create.

191
00:12:12,610 --> 00:12:17,500
OK, let's head over to the next lecture where we actually read in the data that we just created inserted

192
00:12:17,500 --> 00:12:18,400
into the database.

193
00:12:18,790 --> 00:12:19,330
I'll see you there.

