1
00:00:05,330 --> 00:00:09,470
Welcome back, everyone, to this lecture series, we're a first time to talk about updating models

2
00:00:09,710 --> 00:00:12,470
and then we'll talk about updating entries in those models.

3
00:00:13,590 --> 00:00:17,370
So there may come a time when need to create a new column or attribute for a model.

4
00:00:17,490 --> 00:00:23,340
And you can easily update existing models by simply adding a new model class attribute and then migrating

5
00:00:23,340 --> 00:00:24,060
those changes.

6
00:00:25,220 --> 00:00:29,750
You should note, however, that when you're adding new fields, that is attributes to the model class,

7
00:00:29,990 --> 00:00:35,060
the existing entries will need to have some sort of default value inserted, even if it's just a null

8
00:00:35,060 --> 00:00:35,480
value.

9
00:00:35,900 --> 00:00:40,880
In fact, when we attempt to run these migrations without taking care of these sort of default value

10
00:00:40,880 --> 00:00:45,860
issues that are going to occur for the existing entries, Chengdu will actually specifically request

11
00:00:45,860 --> 00:00:50,450
us to make a decision, which is actually quite nice that we don't encounter errors further down the

12
00:00:50,450 --> 00:00:50,780
line.

13
00:00:50,840 --> 00:00:55,820
So jingles actually helping you by making sure you make the correct sort of adjustments for existing

14
00:00:55,820 --> 00:00:56,270
entries.

15
00:00:57,910 --> 00:01:03,010
Now, you're typically given two options when you run those migrations and you haven't chosen a default

16
00:01:03,010 --> 00:01:03,460
value.

17
00:01:03,610 --> 00:01:09,130
You either choose a default value on the spot, right when you create that migration file, or you can

18
00:01:09,130 --> 00:01:12,940
cancel the migration file in general and then create a default value within the model.

19
00:01:13,300 --> 00:01:14,620
It's technically kind of the same thing.

20
00:01:14,620 --> 00:01:15,760
One just delays it.

21
00:01:15,760 --> 00:01:17,270
And has you fixed the model?

22
00:01:17,530 --> 00:01:19,000
The other one just does it on the spot.

23
00:01:19,360 --> 00:01:24,460
I would say it's usually more robust to have the default live in the actual model instead of deciding

24
00:01:24,460 --> 00:01:25,570
they're at the command line.

25
00:01:25,810 --> 00:01:30,160
But keep in mind, each case is different, but again, it's usually better to have some sort of default

26
00:01:30,160 --> 00:01:32,740
value actually defined within your models, not pie.

27
00:01:33,980 --> 00:01:38,990
OK, so let's explore these ideas further in our code, and we're also going to touch on the idea of

28
00:01:38,990 --> 00:01:45,410
using validators within fields which add hardcoded constraints that are going to reject non valid entries.

29
00:01:45,800 --> 00:01:47,210
Let's head back to our code editor.

30
00:01:47,690 --> 00:01:50,450
OK, so here we are back inside of models, not PI.

31
00:01:50,870 --> 00:01:56,030
And let's imagine for our patients, we'd also like to keep track of some sort of biological statistic,

32
00:01:56,040 --> 00:01:57,050
maybe their heart rate.

33
00:01:57,530 --> 00:02:01,310
So every time they come into the office, we go ahead and take their heart rate.

34
00:02:01,610 --> 00:02:06,950
That way, we can take a record in our database and understand what their heart rate was last time or

35
00:02:06,950 --> 00:02:08,090
what their current heart rate is.

36
00:02:08,539 --> 00:02:13,310
Maybe you would want this to be a list, but right now we're just going pretend it's a single integer

37
00:02:13,310 --> 00:02:16,550
that gets updated and rewritten every time there's a patient.

38
00:02:17,000 --> 00:02:20,000
So, for example, I can say heart rate.

39
00:02:21,540 --> 00:02:22,760
Is a new attribute.

40
00:02:23,690 --> 00:02:29,300
For this patient, and it's also going to be an integer, so we're going to say model integer field.

41
00:02:30,570 --> 00:02:34,530
OK, now, if I just leave it like this, there's a couple of things.

42
00:02:34,590 --> 00:02:41,340
One is I would need to decide some sort of default value for this integer, for the existing patients,

43
00:02:41,340 --> 00:02:46,220
because all the existing patients in our database technically don't have some value for heart rate,

44
00:02:46,230 --> 00:02:47,550
we haven't actually assigned that yet.

45
00:02:47,970 --> 00:02:51,750
So let's see what happens if I try to actually make migrations.

46
00:02:52,110 --> 00:02:54,690
So I'm going to save this change in models that PI.

47
00:02:54,690 --> 00:02:55,920
So I had control s there.

48
00:02:56,370 --> 00:03:06,600
And then what I'm going to do is say Python managed our pie, make migrations office hit enter and you'll

49
00:03:06,600 --> 00:03:08,090
see this warning.

50
00:03:08,100 --> 00:03:14,250
Basically, it says it's impossible to add a non knowable field heart rate to patient without specifying

51
00:03:14,250 --> 00:03:14,890
a default.

52
00:03:14,910 --> 00:03:19,740
So this already fantastic that Django is helping us out so much in such clear language, it's saying

53
00:03:20,040 --> 00:03:23,100
this is because the data needs something to populate existing rows.

54
00:03:23,100 --> 00:03:26,610
As I mentioned, the existing patients now actually have any heart rate value.

55
00:03:27,450 --> 00:03:29,610
So then it offers you two options.

56
00:03:29,610 --> 00:03:34,710
If you pass the number one, then it allows you to provide a one off default now, which is going to

57
00:03:34,710 --> 00:03:36,060
be set on all existing rows.

58
00:03:36,060 --> 00:03:38,640
Pay no value for this column or two.

59
00:03:38,670 --> 00:03:41,880
You can quit and manually define a default value in models that pay.

60
00:03:42,390 --> 00:03:48,450
As I mentioned, I would usually recommend you to choose number two here in order to actually hard code

61
00:03:48,450 --> 00:03:49,320
the default value.

62
00:03:49,470 --> 00:03:53,400
Again, every situation is different, so maybe there are going to be instances where you just want

63
00:03:53,400 --> 00:03:59,340
to provide a one off default that overwrites the existing rows with wood having no value.

64
00:03:59,580 --> 00:04:02,040
But in general, I would say go ahead and choose Option two.

65
00:04:02,550 --> 00:04:06,630
This will essentially quit this migration, and you won't have to worry about that migration file.

66
00:04:06,930 --> 00:04:13,650
And now what we can do is inside of integer field, I can set a default and you could do this for any

67
00:04:13,650 --> 00:04:19,260
field, and the default value could be something like the average heart rate of maybe 60 beats per minute.

68
00:04:19,560 --> 00:04:23,280
Now again, it totally depends on the situation of what's a reasonable default or not.

69
00:04:23,610 --> 00:04:27,810
And you can even have a no value as your default, and there's many different ways to doing that.

70
00:04:28,140 --> 00:04:35,040
But you could also say something like null is equal to true, which essentially allows us to just be

71
00:04:35,040 --> 00:04:36,570
blank or a no value.

72
00:04:37,630 --> 00:04:42,700
But I'm going to say my default value is going to be 60 beats per minute.

73
00:04:43,450 --> 00:04:48,100
Now the other thing I want to take into consideration is later on when we are accepting patients and

74
00:04:48,100 --> 00:04:54,880
I'm typing in their first name, last name and age, maybe I want to actually perform some sort of validation

75
00:04:55,180 --> 00:04:56,170
on those numbers.

76
00:04:57,920 --> 00:05:02,870
For example, I don't want someone working at the front desk to accidentally say someone has a heart

77
00:05:02,870 --> 00:05:07,280
rate of or a resting heart rate of something like zero beats per minute because that's impossible.

78
00:05:07,290 --> 00:05:09,140
I mean, that person is basically that.

79
00:05:09,200 --> 00:05:12,680
I also want to make sure that the age range is something reasonable.

80
00:05:13,040 --> 00:05:15,230
This is where validators come into play.

81
00:05:15,710 --> 00:05:23,170
So we end up saying the imports for validation can come for like this, we're going to say from jingo

82
00:05:23,600 --> 00:05:26,840
dark core dot validators.

83
00:05:27,800 --> 00:05:28,490
Imports.

84
00:05:28,520 --> 00:05:33,290
And then there's a bunch of built in validators, and you can check these out if you just Google, search

85
00:05:34,040 --> 00:05:35,160
Django validators.

86
00:05:35,180 --> 00:05:37,550
There is a documentation page on validators.

87
00:05:37,970 --> 00:05:40,610
You can technically write your own custom validators.

88
00:05:40,880 --> 00:05:46,070
But a lot of times the built in ones are more than enough for us, like a URL validator or the fact

89
00:05:46,070 --> 00:05:50,270
that there's min and max value, as well as min and maxlength validators.

90
00:05:50,600 --> 00:05:56,750
So you can see here there's a min value validator, as well as a max length validator.

91
00:05:56,900 --> 00:05:59,330
So let's see how we actually use these going to come back here.

92
00:06:00,860 --> 00:06:06,410
The first thing is we actually import those classes, so I'm going to import Max.

93
00:06:07,880 --> 00:06:15,770
Value validator, and I'm also going to import Min value validator, so from jingo without core that

94
00:06:15,770 --> 00:06:18,260
validators, you import the relevant validators.

95
00:06:18,320 --> 00:06:19,370
Let's step number one.

96
00:06:19,670 --> 00:06:23,630
And if you're wondering what validators are available to you, you simply check the documentation here.

97
00:06:24,350 --> 00:06:31,730
OK, so now that I have the validators ready, then I can go to whatever fields need some sort of validation

98
00:06:31,730 --> 00:06:32,090
check.

99
00:06:32,450 --> 00:06:34,500
And I can say the following like h.

100
00:06:34,610 --> 00:06:38,550
I can say validators is equal to.

101
00:06:39,020 --> 00:06:42,920
And then I just pass any list of the instances of those validators.

102
00:06:43,070 --> 00:06:49,580
So, for example, max value validator, if I take a look at that in the documentation, I can see that

103
00:06:49,580 --> 00:06:53,150
it's a class call and it essentially just takes in a limit value.

104
00:06:53,360 --> 00:06:58,130
You can also rewrite the message that is reported back, but typically the validation error code is

105
00:06:58,130 --> 00:06:59,600
enough and informative enough for you.

106
00:06:59,600 --> 00:07:00,380
Not need to do that.

107
00:07:00,680 --> 00:07:02,780
Instead, just really focus on what's the limiting value.

108
00:07:03,050 --> 00:07:04,280
So what's the max value?

109
00:07:04,400 --> 00:07:05,480
Someone could have for agent?

110
00:07:05,490 --> 00:07:07,460
What's a minimum value someone can have for age?

111
00:07:08,360 --> 00:07:10,550
We come back here and we can say the following.

112
00:07:10,580 --> 00:07:19,970
So my first validator is going to be a min value for essentially an instance of min value validator.

113
00:07:20,750 --> 00:07:23,270
And here I'm going to say the minimum age is zero.

114
00:07:23,450 --> 00:07:24,200
So that's my limit.

115
00:07:24,860 --> 00:07:31,100
Then I add one for max value validator, and this one may be a little trickier because it's like, what's

116
00:07:31,100 --> 00:07:33,620
the maximum age I'm allowing a patient to have?

117
00:07:34,070 --> 00:07:36,530
I'm going to say something like one hundred and twenty.

118
00:07:36,950 --> 00:07:38,390
Maybe that's too young.

119
00:07:38,390 --> 00:07:38,660
Who knows?

120
00:07:38,660 --> 00:07:44,190
In the future we're living to be 130, 150 years old, but for right now, we'll say that's OK.

121
00:07:44,240 --> 00:07:48,110
That way, if someone accidentally types in one hundred and fifty, they're going to get an error and

122
00:07:48,110 --> 00:07:50,360
saying, sorry, that's out of the validation range.

123
00:07:50,510 --> 00:07:54,260
So let's add some validators for the heart rate as well.

124
00:07:54,340 --> 00:07:55,820
So we're going to say validators.

125
00:07:56,940 --> 00:08:01,350
Equals and then we're going to add in some Min Max validators as well.

126
00:08:01,740 --> 00:08:06,030
So I'm going to grab these two validators and let's pass those into our list.

127
00:08:07,450 --> 00:08:11,960
I'm also going to say that the validators that essentially the minimum value, let's say it's I'm the

128
00:08:11,980 --> 00:08:15,130
one heart rate or one beat per minute for their heart rate.

129
00:08:15,460 --> 00:08:20,320
I'm not a doctor, so it'll be kind of up to a specialist to decide what a reasonable minimum and maximum

130
00:08:20,320 --> 00:08:20,980
values here.

131
00:08:21,310 --> 00:08:25,330
And we'll say maybe a maximum value is like three hundred or something.

132
00:08:25,360 --> 00:08:29,650
Obviously, if your heart rate is either three hundred beats per minute or one beats per minute, you

133
00:08:29,860 --> 00:08:31,180
are probably in a serious situation.

134
00:08:31,180 --> 00:08:35,140
But we're going to leave that as kind of just some sort of reality check to make sure we don't get an

135
00:08:35,140 --> 00:08:37,330
entry of like a thousand or something like that.

136
00:08:37,650 --> 00:08:39,730
Essentially, just to make sure we don't get typos later on.

137
00:08:40,090 --> 00:08:42,549
OK, so now we go ahead and save those changes.

138
00:08:42,940 --> 00:08:45,590
And remember, I have a new default value here.

139
00:08:45,610 --> 00:08:50,410
I could technically add defaults for first name, last name and age, but I think heart rate is quite

140
00:08:50,410 --> 00:08:53,580
reasonable to have a default value versus first name.

141
00:08:53,590 --> 00:08:57,400
You kind of don't want a default value there because that assumes that someone is just going to come

142
00:08:57,400 --> 00:08:59,960
in and not give you their first name as a new patient.

143
00:08:59,980 --> 00:09:04,800
So we're gonna expand this again, and let's try running that migrations command again.

144
00:09:04,810 --> 00:09:07,480
So I'm saying Python managed the pie make migrations office.

145
00:09:07,750 --> 00:09:09,910
Remember to save those changes in models, not pie.

146
00:09:09,970 --> 00:09:15,880
So adding in those validators here after we import them and pass them into that list, also just do

147
00:09:15,880 --> 00:09:18,610
a quick double check to make sure parentheses and brackets are OK.

148
00:09:18,910 --> 00:09:22,780
Note that the validators is a list of instances of validation classes.

149
00:09:23,320 --> 00:09:26,710
Then what I'm going to do is go ahead and run, make migrations.

150
00:09:27,220 --> 00:09:27,680
And no.

151
00:09:27,700 --> 00:09:29,800
Now we have a new migrations file.

152
00:09:29,830 --> 00:09:36,020
So as I mentioned underneath the migrations folder, you get these kind of list of historical migrations.

153
00:09:36,040 --> 00:09:36,760
So let's check it out.

154
00:09:36,770 --> 00:09:38,560
What happens when we do another migration?

155
00:09:39,070 --> 00:09:40,710
We can see we have dependencies.

156
00:09:40,720 --> 00:09:42,000
So what are these dependencies?

157
00:09:42,010 --> 00:09:47,260
It's saying it's dependent on the initial file, which is nice because that actually allows you to then

158
00:09:47,530 --> 00:09:51,080
in the future, roll back changes in case you want to undo this migration.

159
00:09:51,640 --> 00:09:56,050
And then you can see here it's going to add fields like it's adding a new heart rate field and it's

160
00:09:56,050 --> 00:09:57,250
also altering field.

161
00:09:57,310 --> 00:10:01,900
It's altering the existing field, and it's just altering it with some validators.

162
00:10:02,080 --> 00:10:03,820
So this is actually quite readable.

163
00:10:03,820 --> 00:10:08,560
Again, as I mentioned, we don't typically edit these migration files, but it is nice to understand

164
00:10:08,560 --> 00:10:10,030
what's going on inside of them.

165
00:10:10,570 --> 00:10:15,790
So once we're OK with that again, we typically don't check that migration file was actually going to

166
00:10:15,790 --> 00:10:16,690
run this migration.

167
00:10:16,690 --> 00:10:23,740
So we're going to say Python managed that pie migrate to enter, and then it applied those migrations.

168
00:10:24,130 --> 00:10:24,460
OK.

169
00:10:24,850 --> 00:10:28,510
So that's the very basics of updating your model.

170
00:10:28,540 --> 00:10:35,410
Again, you just add in attributes and you may need to think about what defaults are reasonable given

171
00:10:35,410 --> 00:10:36,190
what you're working with.

172
00:10:36,490 --> 00:10:38,890
Ian, we also talked about adding in validators.

173
00:10:39,190 --> 00:10:44,050
Coming up next, we're going to talk about updating existing entries in our database table.

174
00:10:44,320 --> 00:10:44,860
I'll see you there.

