1
00:00:05,200 --> 00:00:06,190
Welcome back, everyone.

2
00:00:06,220 --> 00:00:11,350
And this lecture will talk about a couple of customisation options within the Django model form.

3
00:00:11,620 --> 00:00:15,700
I want to point out there's a lot more to model forms than what we're going to show here, and you can

4
00:00:15,700 --> 00:00:19,600
check out the documentation for even more features that are available to you.

5
00:00:19,810 --> 00:00:25,000
For model forms, but right now, we'll just go through some really common options that we can pass

6
00:00:25,000 --> 00:00:27,760
on as attributes inside the meta subclass.

7
00:00:28,420 --> 00:00:31,330
OK, so here I am back inside Visual Studio code.

8
00:00:31,690 --> 00:00:39,220
If we come to Forms DOP II, remember that we now have this model form with this meta subclass and we

9
00:00:39,220 --> 00:00:41,440
were able to manually define the fields.

10
00:00:41,890 --> 00:00:46,750
For example, if I only wanted the first name and the last name, I could just pass that in as a list.

11
00:00:46,990 --> 00:00:53,040
However, it's really common to want all the fields within a model to be within the fields of a form

12
00:00:53,050 --> 00:00:53,980
that is the model form.

13
00:00:54,640 --> 00:00:56,500
If you want that, you can say the following.

14
00:00:58,220 --> 00:01:03,860
In string, you say, underscore, underscore, all underscore, underscore.

15
00:01:04,280 --> 00:01:11,300
And that's essentially automatically the same thing as saying pass in all the model fields as form fields.

16
00:01:12,050 --> 00:01:12,380
OK.

17
00:01:12,650 --> 00:01:17,840
And we probably actually won't see a difference here if I refresh the page because essentially the same

18
00:01:17,840 --> 00:01:20,000
thing first name, last name and stars.

19
00:01:20,540 --> 00:01:25,070
What I do want to point out, however, is we can see there's some labels automatically created for

20
00:01:25,070 --> 00:01:27,410
you first name, last name and stars.

21
00:01:27,860 --> 00:01:30,740
Let's imagine we wanted to overwrite these labels.

22
00:01:31,400 --> 00:01:39,080
So what we can do is come back to the meta subclass and create a dictionary called labels.

23
00:01:40,780 --> 00:01:47,380
And in this dictionary, you essentially pass in the attribute name from the model like first underscore

24
00:01:47,410 --> 00:01:52,720
name and then the actual label you want to show up on the form.

25
00:01:52,870 --> 00:01:56,410
For example, I can say your first name in all caps.

26
00:01:58,360 --> 00:02:05,590
First name there we go, and we can do the same for the next one, so last name attribute can say last

27
00:02:05,590 --> 00:02:05,980
name.

28
00:02:06,820 --> 00:02:13,270
And then finally, we can also say stars, so stars attribute, let's just call that instead rating.

29
00:02:16,540 --> 00:02:17,590
So I'm going to save that.

30
00:02:19,710 --> 00:02:25,440
So come here, save those, and so now I should see these as the label, so again, the keys are the

31
00:02:25,440 --> 00:02:28,890
actual model attributes, first name, last name stars.

32
00:02:29,220 --> 00:02:31,860
And then this is the strings you actually want to show up on the web page.

33
00:02:32,250 --> 00:02:34,560
So I come back here to rental review form.

34
00:02:35,010 --> 00:02:40,280
Refresh this and I see in all caps your first name, last name and then rating.

35
00:02:40,770 --> 00:02:48,750
OK, now what's kind of interesting is if I try to submit something that maybe is greater than five

36
00:02:49,440 --> 00:02:51,000
than currently, it works.

37
00:02:51,180 --> 00:02:57,420
But let's imagine I actually added validators to my model to limit this so I could come back to models.

38
00:02:57,840 --> 00:03:02,370
And remember, there's a whole idea of core model value validator.

39
00:03:02,380 --> 00:03:03,660
So for example, I can say from.

40
00:03:04,800 --> 00:03:10,150
Django decor, the validators, and we've actually seen these before.

41
00:03:10,530 --> 00:03:18,450
I can get the min and max value validators, so I have a min value validator and a max value validator.

42
00:03:18,840 --> 00:03:23,340
And so as you're developing your model, maybe you can only have between one and five stars.

43
00:03:23,640 --> 00:03:31,320
So you say you're validators is an argument here and you pass those in as a list so I can get my men.

44
00:03:32,510 --> 00:03:35,420
Value validator pass in a minimum value of one.

45
00:03:36,470 --> 00:03:43,580
And then a max value validator and passing a minimum value of five, and sorry for all those pop ups

46
00:03:43,580 --> 00:03:44,630
that were covering the screen.

47
00:03:44,960 --> 00:03:47,090
But you get the idea again, we've seen this before.

48
00:03:47,100 --> 00:03:51,530
You just say from Django, the core validators and there's a bunch of validators you can look up in

49
00:03:51,530 --> 00:03:56,780
the documentation and you already know you can develop models those ways in order to have validation.

50
00:03:56,900 --> 00:04:02,510
And these validators, when you end up saving these changes and if you come back to views, those are

51
00:04:02,510 --> 00:04:04,130
automatically going to be checked for you.

52
00:04:04,220 --> 00:04:06,140
Inside is valid.

53
00:04:06,680 --> 00:04:14,660
So now models that PI has these men value and max value validators, however, haven't technically migrated

54
00:04:14,660 --> 00:04:15,530
those changes yet.

55
00:04:15,980 --> 00:04:18,260
So I need to quit the server.

56
00:04:19,320 --> 00:04:23,190
Say Python managed that pie, make migrations.

57
00:04:24,350 --> 00:04:30,890
And I should see, OK, I have altered the stars on review to have these validators here then actually

58
00:04:30,890 --> 00:04:35,510
need to run those migrations, python, manage the pie, migrate.

59
00:04:36,290 --> 00:04:38,120
And now I've altered those.

60
00:04:38,510 --> 00:04:40,610
So now actually have these validators existing.

61
00:04:41,060 --> 00:04:48,440
Let's see what happens when I rerun my website, but try to provide a star rating that's above five.

62
00:04:49,220 --> 00:04:53,900
So going to come back here, come back to rental review form again.

63
00:04:53,900 --> 00:04:56,330
Say, Let's say John Smith.

64
00:04:58,010 --> 00:05:04,100
And this time trying to say something that's above five, you can hit submit query and notice now nothing's

65
00:05:04,100 --> 00:05:04,850
actually happening.

66
00:05:05,390 --> 00:05:08,930
Well, what's going on is this form is no longer valid.

67
00:05:09,380 --> 00:05:15,530
So each time I try to submit this query, you'll notice it's going to give you a two step code.

68
00:05:15,860 --> 00:05:23,960
And if I come back to my admin view, if I were to go to cars reviews, I don't see a dozen reviews

69
00:05:23,960 --> 00:05:24,110
there.

70
00:05:24,110 --> 00:05:29,300
I just see the few ones that I've made this one I made before I actually migrated those changes to the

71
00:05:29,300 --> 00:05:29,630
model.

72
00:05:30,530 --> 00:05:34,280
So the question is, is there a way I can actually report back to the user?

73
00:05:34,610 --> 00:05:38,930
Hey, here's a little error message and you should, you know, show them something.

74
00:05:39,350 --> 00:05:44,990
Well, luckily for us, that's actually also done automatically through Django and Django forms.

75
00:05:45,140 --> 00:05:50,090
And what we're going to do first is make sure we're reporting that error inside the HTML.

76
00:05:51,590 --> 00:05:57,020
So if I come back to my HTML file, that's actually showing this form.

77
00:05:57,620 --> 00:06:00,350
So here I have rental review that HTML.

78
00:06:00,710 --> 00:06:05,330
Remember, right now I'm showing the label tag and the field.

79
00:06:06,050 --> 00:06:09,920
So there's another attribute called field errors.

80
00:06:10,130 --> 00:06:13,160
So what I can also say here is pass in.

81
00:06:15,810 --> 00:06:17,160
Field Dot.

82
00:06:18,210 --> 00:06:25,950
Errors save those changes, and now when you come back to the form, let's go ahead and do a test submission

83
00:06:25,950 --> 00:06:28,600
like for I'm going to refresh this real quick.

84
00:06:29,010 --> 00:06:31,700
And it's going to ask you, Hey, do you want to resend this information?

85
00:06:31,710 --> 00:06:32,730
Go ahead and say Resend.

86
00:06:33,210 --> 00:06:36,480
And actually recent the last pieces of information.

87
00:06:36,810 --> 00:06:38,850
So I'm going to say John Smith rating of four.

88
00:06:38,880 --> 00:06:39,210
Notice.

89
00:06:39,210 --> 00:06:40,140
I have a little warning here.

90
00:06:40,360 --> 00:06:42,150
I'm going to submit that worked.

91
00:06:42,690 --> 00:06:45,150
Let's come back here and now try to.

92
00:06:45,180 --> 00:06:48,630
I'm going to actually refresh this to cars.

93
00:06:48,630 --> 00:06:48,960
What's?

94
00:06:50,050 --> 00:06:55,180
Let's go rental review and now we have a fresh setting.

95
00:06:55,400 --> 00:06:59,800
OK, what happened was actually he probably should have just restarted the server because I did an HTML

96
00:06:59,800 --> 00:07:00,400
change here.

97
00:07:00,670 --> 00:07:01,630
But you get the idea.

98
00:07:01,870 --> 00:07:04,540
OK, let's let's bring in the review form.

99
00:07:05,230 --> 00:07:05,530
OK.

100
00:07:05,890 --> 00:07:09,820
So I have rental review form your first name, last name and rating.

101
00:07:10,090 --> 00:07:14,020
And what I added now was the fact that, hey, I'm going to actually report errors.

102
00:07:14,510 --> 00:07:18,580
So luckily for us, I can't pass on a name, so let's say.

103
00:07:19,610 --> 00:07:23,720
Joe Poe, and this is now going to be something that is no longer valid.

104
00:07:23,840 --> 00:07:25,610
Eight stars above five.

105
00:07:25,940 --> 00:07:31,250
If you submit the query, you notice there's this automatic unordered list item that shows up for you

106
00:07:31,520 --> 00:07:34,850
that says ensure this value is less than or equal to five.

107
00:07:35,540 --> 00:07:41,690
That's pretty incredible because what this is doing is it's automatically generating an error message

108
00:07:41,690 --> 00:07:47,690
for you, simply based off the max or min validators from this field.

109
00:07:48,260 --> 00:07:54,140
So that is a ton of heavy lifting that Django is doing for you, just from models up high.

110
00:07:54,890 --> 00:07:59,360
You should really take a time here to kind of in all of the workflow that jingles doing for you.

111
00:07:59,390 --> 00:08:02,960
Django is looking into your models, checking the validators of those models.

112
00:08:03,290 --> 00:08:11,600
Then recall we passed in that model to a model form that then gets sent to the HTML through the actual

113
00:08:11,600 --> 00:08:15,800
view, and you can easily call those errors for the person to see.

114
00:08:16,160 --> 00:08:20,720
And if you wanted to, you could put this in front of an if statement to maybe even add in more information

115
00:08:20,720 --> 00:08:25,940
to the user, like if field errors exist, show something or so a message to the user.

116
00:08:26,480 --> 00:08:32,900
Now what you can also do is beyond just edit the way this shows up through HTML.

117
00:08:32,900 --> 00:08:37,370
By default, it's an unordered list, but obviously you could edit the HTML to make this look like something

118
00:08:37,370 --> 00:08:37,669
else.

119
00:08:37,880 --> 00:08:43,429
And later on, we will discuss Django messages in general, but this is an automated piece of text that

120
00:08:43,429 --> 00:08:44,960
is basically linked to the validator.

121
00:08:45,230 --> 00:08:48,420
You could add in your own error messages.

122
00:08:48,480 --> 00:08:56,750
So the way that works is you come over here to the forms and just like you have labels, you can customize

123
00:08:56,750 --> 00:09:01,730
with the attribute error underscore messages.

124
00:09:03,490 --> 00:09:09,130
And then you decide the field you're interested in, in this case, I'm interested in stars field and

125
00:09:09,130 --> 00:09:17,860
then with that, you add in a key value pairing that has to do with the actual error message.

126
00:09:18,250 --> 00:09:20,270
This one's a little trickier to figure out.

127
00:09:20,290 --> 00:09:26,200
You do have to reference the documentation for the built in field classes and their possible error codes.

128
00:09:26,710 --> 00:09:34,240
So if you go to the documentation on if I bring it over here, built in field classes, they'll talk

129
00:09:34,240 --> 00:09:40,660
about the fields, the default widgets, and they'll also talk about the error message keys.

130
00:09:40,840 --> 00:09:45,610
So for example, we were on an integer field, so I'll go ahead and look that one up.

131
00:09:46,060 --> 00:09:48,190
Let's find the integer field on the page.

132
00:09:49,290 --> 00:09:55,260
And I see, OK, I have entered your field that gives you a default widget of a no input.

133
00:09:56,040 --> 00:10:02,160
And we can see if we scroll down, there were these validators and essentially error messages have to

134
00:10:02,160 --> 00:10:05,610
do with max value and min value.

135
00:10:05,850 --> 00:10:11,970
So the error message keys, which is what we're interested in, is max underscore value and min underscore

136
00:10:11,970 --> 00:10:12,420
value.

137
00:10:12,720 --> 00:10:16,080
So you don't actually call validators, you call these keys.

138
00:10:16,530 --> 00:10:18,780
So it's a lot to look up in the documentation.

139
00:10:18,780 --> 00:10:25,050
But essentially, each field beyond just having a widget also has error message keys available for you.

140
00:10:25,350 --> 00:10:27,840
And so the ones we're interested here are max value and min value.

141
00:10:28,200 --> 00:10:31,710
And the reason they're called error message keys is because they're going to be keys in this dictionary.

142
00:10:31,890 --> 00:10:32,730
So I can say.

143
00:10:33,970 --> 00:10:34,780
Men value.

144
00:10:35,830 --> 00:10:42,100
And then I can write my custom error message to make it obvious, I'm going to say, yo, then value

145
00:10:42,100 --> 00:10:44,920
is one, and then we'll say comma here.

146
00:10:45,220 --> 00:10:47,650
And let's now see max value for that key.

147
00:10:48,710 --> 00:10:50,240
And this one's going to be an obvious.

148
00:10:51,510 --> 00:10:53,640
Yo yo, max value is five.

149
00:10:54,270 --> 00:11:00,240
OK, go ahead and save those changes, and what I'm going to do here is I'm actually going to restart

150
00:11:01,140 --> 00:11:01,860
my server.

151
00:11:02,730 --> 00:11:06,360
And then let's come back to our rental review.

152
00:11:07,300 --> 00:11:09,310
So we'll come back to our rental review form.

153
00:11:10,600 --> 00:11:13,100
And again, it says first name, last name and reading.

154
00:11:13,690 --> 00:11:18,130
So let's say again, your poll here and let's give this a rating that's above five.

155
00:11:18,550 --> 00:11:19,450
Hit submit query.

156
00:11:19,720 --> 00:11:23,740
And now we have our custom message Yo max value is five.

157
00:11:24,400 --> 00:11:28,180
OK, so pretty amazing stuff that Django is doing for you.

158
00:11:28,540 --> 00:11:32,320
And there's a lot more to model forms than what we've discussed here.

159
00:11:32,620 --> 00:11:38,230
But this is essentially the key basics in order for you to connect a model quickly to a form and even

160
00:11:38,230 --> 00:11:40,060
begin to customize it quite a bit.

161
00:11:40,510 --> 00:11:45,370
And again, a lot of the things that were available to us within normal forms are available to us often

162
00:11:45,370 --> 00:11:46,090
model forms.

163
00:11:46,540 --> 00:11:51,730
For right now, we're going to leave it at that because we should really learn about class based views

164
00:11:51,730 --> 00:11:58,210
before we dive too deep into model forms, because class-based views are essentially going to automatically

165
00:11:58,210 --> 00:12:02,860
generate web pages for us with forms from a model class.

166
00:12:02,890 --> 00:12:09,580
But if you are interested in more details of model forms, the context on the actual documentation page

167
00:12:09,580 --> 00:12:10,730
is really amazing.

168
00:12:10,750 --> 00:12:15,640
They basically give you examples of every little thing you would want to do with a model form.

169
00:12:15,730 --> 00:12:17,440
So I highly encourage you to check it out.

170
00:12:17,710 --> 00:12:24,490
But hopefully just see right now how powerful this tool is for instantly creating a form for the HTML

171
00:12:24,490 --> 00:12:25,420
from the model.

172
00:12:26,140 --> 00:12:32,080
So as a final review, again to remember we have our normal models and then our model forms, which

173
00:12:32,080 --> 00:12:37,840
we end up passing in through the views we should check if it's a post requests, render that and then

174
00:12:37,840 --> 00:12:38,620
check if it's valid.

175
00:12:38,930 --> 00:12:41,980
Otherwise, create the form and then pass in a dictionary.

176
00:12:42,340 --> 00:12:47,560
And then we already know all the different variety of things we can do inside the templates itself.

177
00:12:47,950 --> 00:12:53,020
And typically we're going to do is try to aim for the HTML to be as simple as possible and more of the

178
00:12:53,020 --> 00:12:55,060
work being done in Python.

179
00:12:55,330 --> 00:13:00,460
But because we're a python Django, of course, not really an HTML focus course, but obviously you

180
00:13:00,460 --> 00:13:02,650
could make the workload happen on either end.

181
00:13:03,100 --> 00:13:03,430
All right.

182
00:13:03,460 --> 00:13:04,390
That's it for this lecture.

183
00:13:04,510 --> 00:13:05,350
I'll see it the next one.

