1
00:00:00,140 --> 00:00:06,950
In the previous video, we set up a project and an app and we see this on the front end and now it's

2
00:00:06,950 --> 00:00:12,290
time to code the app and we will continue with a bottom, top approach.

3
00:00:12,290 --> 00:00:16,970
Bottom meaning that we will first build the database model.

4
00:00:16,970 --> 00:00:21,800
So the database model is like the structure of your web apps.

5
00:00:21,830 --> 00:00:28,520
It defines the data so I can show you the end product.

6
00:00:28,610 --> 00:00:33,500
So this will be the end product, what we will build at the end.

7
00:00:33,500 --> 00:00:37,310
So that's how our web app will look like on the front end.

8
00:00:37,310 --> 00:00:40,670
So now we can see what data we have here, right?

9
00:00:40,850 --> 00:00:45,980
And based on that, we can define a database model.

10
00:00:45,980 --> 00:00:48,680
So what data do we have here?

11
00:00:48,890 --> 00:00:56,720
Well, I can see that we have like categories like starters and salads and main dishes, right?

12
00:00:57,140 --> 00:00:59,780
So that's one data sets.

13
00:00:59,780 --> 00:01:00,630
We have.

14
00:01:00,660 --> 00:01:04,260
Then we have meals like pizza and Greek salads.

15
00:01:04,290 --> 00:01:07,380
Then we have descriptions for each meal.

16
00:01:07,380 --> 00:01:09,990
Then we have prices for each meal.

17
00:01:09,990 --> 00:01:15,300
So these dots are what I just mentioned category meal descriptions.

18
00:01:15,300 --> 00:01:20,580
Each of these will be a database table, field a column in the database.

19
00:01:20,580 --> 00:01:24,390
So let's go ahead and create those columns.

20
00:01:24,540 --> 00:01:31,140
I'll just stop the app, go to the terminal, press control C to stop your app from running.

21
00:01:31,260 --> 00:01:37,260
Then you want to go to restaurant menu directory and then double click on Models.py.

22
00:01:38,140 --> 00:01:40,660
And we want to create the model in here.

23
00:01:40,990 --> 00:01:50,130
As I explained in the previous app of the course where we use Django, what we do is we write a class,

24
00:01:50,140 --> 00:01:53,590
let's call this item models dot model.

25
00:01:53,590 --> 00:02:00,790
So this inherits from the models library, from the model class of the models library.

26
00:02:03,330 --> 00:02:09,870
And in this class we want to define a series of class variables.

27
00:02:09,870 --> 00:02:17,220
So meal is the first variable, a class variable I have just created here, which will be equal to models

28
00:02:17,220 --> 00:02:19,230
dot care field.

29
00:02:19,230 --> 00:02:19,860
Right?

30
00:02:20,220 --> 00:02:30,030
So with this first class variable, I am instructing Django to create a column in the table of our database.

31
00:02:30,030 --> 00:02:34,410
The database will be generated when we run the command here.

32
00:02:34,410 --> 00:02:41,160
Python manage.py make migrations will do that later, not now.

33
00:02:41,280 --> 00:02:48,780
And that command will get this class will read everything we have here and it will create one column

34
00:02:48,780 --> 00:02:51,030
in the table for each of these variables.

35
00:02:51,030 --> 00:02:56,880
So the first one is will be like text containing the meal.

36
00:02:56,880 --> 00:02:59,460
Let's set a maximum length of.

37
00:03:00,320 --> 00:03:01,820
The 1000 characters.

38
00:03:03,650 --> 00:03:06,320
Let's set this as unique.

39
00:03:06,320 --> 00:03:13,430
So this will protect the database table from having duplicate values, duplicate meals.

40
00:03:13,430 --> 00:03:16,730
So that's something we don't want to happen, right?

41
00:03:16,970 --> 00:03:22,250
So think of it from the cook perspective and from the restaurant customers.

42
00:03:22,250 --> 00:03:26,630
So the cook wants to have unique meals, right?

43
00:03:26,660 --> 00:03:32,750
If you have a bean soup, you want to have one bean soup, you don't want to have two bean soup because

44
00:03:32,750 --> 00:03:34,400
that doesn't make any sense.

45
00:03:34,610 --> 00:03:38,390
So this is like the restriction we give to this database.

46
00:03:38,390 --> 00:03:43,160
Field description will be the next field.

47
00:03:43,160 --> 00:03:47,480
So that will represent this here.

48
00:03:48,830 --> 00:03:54,860
So that will be the description or the ingredients, if you like to call it like that.

49
00:03:55,410 --> 00:04:01,340
So this will describe that meal, that will describe that meal and so on.

50
00:04:01,350 --> 00:04:04,800
So the description will be models that.

51
00:04:06,500 --> 00:04:07,760
Care fields again.

52
00:04:07,760 --> 00:04:10,670
Let's give it a maximum length of.

53
00:04:12,130 --> 00:04:13,360
2000.

54
00:04:14,410 --> 00:04:22,600
In this case, having it set to unique is not very meaningful because it could happen that that different

55
00:04:22,600 --> 00:04:24,940
meals could have the same ingredients.

56
00:04:24,940 --> 00:04:25,430
Right.

57
00:04:25,450 --> 00:04:29,530
So you don't want to set a unique parameter to true in here.

58
00:04:30,280 --> 00:04:32,770
Price is the next one.

59
00:04:32,770 --> 00:04:34,450
Let's do models again.

60
00:04:34,450 --> 00:04:38,350
Dot This is a number.

61
00:04:38,350 --> 00:04:43,870
So you want to use a data type which works well with numbers.

62
00:04:43,870 --> 00:04:52,690
So decimal fields, you want to have decimal places of two, so two decimal points.

63
00:04:53,440 --> 00:04:56,410
So we have meal, we have description, we have price.

64
00:04:56,410 --> 00:05:00,730
We also said we want the categories, so let's have this.

65
00:05:02,890 --> 00:05:07,150
Set as meal type or category wherever you like.

66
00:05:07,510 --> 00:05:08,740
Models dot.

67
00:05:11,070 --> 00:05:11,640
Car.

68
00:05:13,790 --> 00:05:23,060
It's better to have like some choices in here and have it equal to this variable mill type.

69
00:05:23,060 --> 00:05:29,750
And that variable will be defined in here mill type.

70
00:05:30,020 --> 00:05:37,520
So that's going to be a tuple of tuples and each tuple will have two strings.

71
00:05:38,840 --> 00:05:47,770
The first one will be a string with lowercase letters and the second will be a strings.

72
00:05:47,780 --> 00:05:52,670
With the first letter uppercase, You'll see why.

73
00:05:53,750 --> 00:05:55,040
So we have starters.

74
00:05:55,040 --> 00:05:57,470
We have salads.

75
00:05:59,540 --> 00:06:02,540
Salads, a comma.

76
00:06:03,020 --> 00:06:05,540
So it's a tuple of tuples, right?

77
00:06:05,870 --> 00:06:09,140
Each tuple is an item of this main tuple.

78
00:06:09,170 --> 00:06:14,750
Therefore, each item should be separated by a comma with the other items.

79
00:06:14,750 --> 00:06:17,210
So comma, another tuple.

80
00:06:17,690 --> 00:06:20,120
We have main dishes.

81
00:06:20,270 --> 00:06:22,400
So main underscore dishes.

82
00:06:22,430 --> 00:06:25,400
It's good to not to have spaces there.

83
00:06:26,030 --> 00:06:27,790
Main dishes.

84
00:06:27,800 --> 00:06:34,070
So perhaps you're starting to understand that this will be displayed on the front end, this part,

85
00:06:34,730 --> 00:06:38,240
and this will be used by the code inside.

86
00:06:38,270 --> 00:06:40,220
You'll see how exactly.

87
00:06:40,370 --> 00:06:41,540
And lastly.

88
00:06:43,330 --> 00:06:45,250
Desserts, everyone.

89
00:06:45,250 --> 00:06:50,130
I mean, not everyone, but most people want a dessert after the meal.

90
00:06:50,140 --> 00:06:52,000
So that's what we have here.

91
00:06:52,570 --> 00:06:54,820
That's our tuple of tuples, of strings.

92
00:06:55,620 --> 00:06:58,380
So why did we use some choices in here?

93
00:06:58,380 --> 00:07:00,040
Some predefined choices?

94
00:07:00,060 --> 00:07:01,890
Well, that's a better decision.

95
00:07:01,920 --> 00:07:03,750
A better way to do this.

96
00:07:03,750 --> 00:07:13,010
Because when the cooks use the admin interface to add meals, they can add all kinds of meals there.

97
00:07:13,020 --> 00:07:15,630
So that's more like an infinite choices.

98
00:07:15,630 --> 00:07:25,130
They have to add meals, but the categories is more like a finite data type where you have like four,

99
00:07:25,170 --> 00:07:29,610
five, six categories like starter salads, main dishes, desserts.

100
00:07:29,610 --> 00:07:39,270
So the cooks in the admin interface can can use like a dropdown list to select from the existing choices

101
00:07:39,270 --> 00:07:42,480
instead of creating their own categories.

102
00:07:42,480 --> 00:07:43,110
Right.

103
00:07:43,140 --> 00:07:44,910
I hope you see the difference here.

104
00:07:45,780 --> 00:07:50,070
So that's a good thing to do when you have a finite list of values.

105
00:07:50,790 --> 00:07:51,600
Right?

106
00:07:51,630 --> 00:07:52,710
Next.

107
00:07:54,470 --> 00:08:02,180
It would be good to actually have an author name attached to each meal.

108
00:08:02,180 --> 00:08:09,620
So perhaps you have more than one cook who will access the admin interface, who can add meals.

109
00:08:09,620 --> 00:08:13,100
So it's good to to know who added what.

110
00:08:13,130 --> 00:08:18,080
So in that case, we also add this author fields in the database table.

111
00:08:21,390 --> 00:08:27,360
I'm using a foreign key here, followed by user, which is something we need to import.

112
00:08:27,360 --> 00:08:29,870
And I'll explain you why this.

113
00:08:29,880 --> 00:08:39,900
So first let's import from Django dot contrib dot auth dot models import user.

114
00:08:39,900 --> 00:08:41,610
So this is a class.

115
00:08:41,970 --> 00:08:46,590
This class represents a database model.

116
00:08:46,590 --> 00:08:48,570
The user database model.

117
00:08:48,600 --> 00:08:57,480
So just like item is a table in the database, user is also a table in the database which contains the

118
00:08:57,480 --> 00:09:01,410
username, the user password as fields as columns.

119
00:09:01,410 --> 00:09:02,100
Right?

120
00:09:02,250 --> 00:09:12,420
So what we're doing here is we are creating a relationship between item this table and the user table.

121
00:09:12,810 --> 00:09:23,070
In this case, this is a many to one relationship, which means that many items can be associated with

122
00:09:23,070 --> 00:09:28,530
one user or in other words, one user can be associated to many items.

123
00:09:28,530 --> 00:09:31,140
So one cook the user is the cook, right?

124
00:09:31,170 --> 00:09:35,400
One cook can create more than one item.

125
00:09:35,400 --> 00:09:36,990
An item is a meal, right?

126
00:09:37,020 --> 00:09:39,360
The meal has a name, a description.

127
00:09:39,360 --> 00:09:42,840
So the ingredients, the price, the category.

128
00:09:42,840 --> 00:09:47,760
And it will also contain the user so many to one.

129
00:09:47,760 --> 00:09:50,880
So one cook can create many meals.

130
00:09:50,880 --> 00:09:52,770
That's the relationship in here.

131
00:09:53,760 --> 00:09:57,810
Then what happens is a user is deleted.

132
00:09:57,810 --> 00:10:08,490
So you want to specify that behavior here with an own delete parameter if you want that all meals that

133
00:10:08,490 --> 00:10:12,120
a user added should be deleted.

134
00:10:12,210 --> 00:10:14,310
You want to pass models.

135
00:10:14,310 --> 00:10:15,690
Dot Cascade.

136
00:10:16,110 --> 00:10:17,010
Cascade.

137
00:10:19,280 --> 00:10:20,190
Kate.

138
00:10:20,600 --> 00:10:28,970
So if Cook John is deleted from the user's table, this can be done in the admin interface.

139
00:10:28,970 --> 00:10:29,600
Right?

140
00:10:30,110 --> 00:10:37,160
If John is deleted, this will delete all the meals that John had created.

141
00:10:37,190 --> 00:10:44,030
If that is something you don't want to do, then instead of cascades you want to say protect.

142
00:10:45,050 --> 00:10:53,930
In that case you'll not be able to delete the user and therefore the meals will also not be deleted.

143
00:10:54,260 --> 00:10:59,720
Another option would be to use set underscore null.

144
00:10:59,840 --> 00:11:08,810
That means if the author is deleted, then if the items table where you have all the meals and you have

145
00:11:08,810 --> 00:11:14,800
the author field that that author field will be set from John to null.

146
00:11:14,810 --> 00:11:20,520
So no value but the meals, the records will still be there, right?

147
00:11:20,550 --> 00:11:23,910
However, the author name will not be associated.

148
00:11:24,300 --> 00:11:25,740
I hope that makes sense.

149
00:11:25,950 --> 00:11:27,960
I'll go for just protect.

150
00:11:28,950 --> 00:11:31,590
I think that's a better choice here.

151
00:11:32,580 --> 00:11:37,710
Then we have a we can create a status field as well.

152
00:11:38,250 --> 00:11:39,570
Models Dot.

153
00:11:39,630 --> 00:11:41,370
Integer fields.

154
00:11:41,370 --> 00:11:43,050
I'll explain what this does.

155
00:11:43,080 --> 00:11:49,760
Choices again, is equal to status, and default is equal to zero.

156
00:11:49,770 --> 00:12:00,180
And then we create a status variable up here above the class status is a variable, a tuple of tuples.

157
00:12:00,330 --> 00:12:04,740
Each tuple will have two items zero

158
00:12:07,110 --> 00:12:10,110
unavailable, comma one.

159
00:12:12,380 --> 00:12:13,160
Available.

160
00:12:13,160 --> 00:12:21,470
So by default the meal will be unavailable on or you can set it to available wherever you like.

161
00:12:22,310 --> 00:12:29,570
So remember I told you, I showed you that some meals like Panacotta here is unavailable.

162
00:12:29,570 --> 00:12:33,010
So that is the field that defines that.

163
00:12:33,020 --> 00:12:37,670
And so the cooks can use the admin interface to set.

164
00:12:38,440 --> 00:12:40,090
Either 0 or 1.

165
00:12:40,120 --> 00:12:41,980
The status of a meal.

166
00:12:42,010 --> 00:12:46,220
So again, we have a finite number of values.

167
00:12:46,250 --> 00:12:51,190
Therefore, we use choices just like we did with the meal type up here.

168
00:12:51,610 --> 00:12:54,980
Lastly, we'll add two more fields here.

169
00:12:55,000 --> 00:13:00,340
Date created first and then date updated.

170
00:13:00,370 --> 00:13:07,660
So the date created will be a field from models dot date time field.

171
00:13:07,900 --> 00:13:11,440
Same goes for the other field.

172
00:13:11,590 --> 00:13:19,660
So these two now will have an auto now add set to true.

173
00:13:19,780 --> 00:13:25,230
And this one will have an auto now set to true.

174
00:13:25,240 --> 00:13:28,570
So that is what distinguishes these two fields.

175
00:13:29,590 --> 00:13:33,700
This will generate date time stamps.

176
00:13:33,700 --> 00:13:41,450
So whenever a cook adds a meal, an item right with a meal and description and price and meal type,

177
00:13:41,540 --> 00:13:47,090
the time stamp will be recorded in one of the table fields.

178
00:13:47,420 --> 00:13:51,860
So that parameter makes it possible to record the time stamp.

179
00:13:51,860 --> 00:13:59,360
And this other parameter records when the item is edited, when it's updated.

180
00:13:59,420 --> 00:14:03,620
So I think that's self-explanatory what these two will do.

181
00:14:03,650 --> 00:14:06,830
You'll see everything in the table later on anyway.

182
00:14:07,490 --> 00:14:09,970
And that's about the class variables.

183
00:14:09,980 --> 00:14:17,680
And lastly, let's define the STR methods, which is something we also did previously in the previous

184
00:14:17,690 --> 00:14:18,230
app.

185
00:14:18,500 --> 00:14:24,590
And this will return just the self dot meal class variable.

186
00:14:24,590 --> 00:14:32,450
So whenever the item is printed out in the admin interface, it will be represented by the meal name.

187
00:14:32,450 --> 00:14:32,870
Right.

188
00:14:32,900 --> 00:14:34,460
Being soup for example.

189
00:14:36,270 --> 00:14:37,350
Yep, that's it.

190
00:14:37,860 --> 00:14:39,810
So we have defined the code.

191
00:14:39,810 --> 00:14:46,950
We have designed the database model, but we haven't physically created a database yet.

192
00:14:46,980 --> 00:14:49,710
To do that, you want to run Python?

193
00:14:52,360 --> 00:14:56,770
Manage.py make migrations.

194
00:14:56,770 --> 00:15:01,990
So this commands in the terminal execute that.

195
00:15:02,320 --> 00:15:10,150
I've got an error here in the models file choice in this line.

196
00:15:12,140 --> 00:15:14,470
That's the models file 22.

197
00:15:14,480 --> 00:15:16,250
So it should be choices.

198
00:15:16,460 --> 00:15:17,990
So that was a typo.

199
00:15:18,740 --> 00:15:26,180
So what's happening here, as you can see, is that when we run this command, Django reads this file

200
00:15:26,180 --> 00:15:29,420
and it tries to create a database table.

201
00:15:29,420 --> 00:15:30,800
So let's run it again.

202
00:15:31,610 --> 00:15:34,550
Oh, again, I've got a typo in the command this time.

203
00:15:34,550 --> 00:15:37,370
Python manage.py make migrations.

204
00:15:37,880 --> 00:15:41,390
This time we get a max length.

205
00:15:41,390 --> 00:15:48,110
Should be contained by calf fields in the meal type attribute.

206
00:15:48,140 --> 00:15:49,940
Yeah, we forgot to add that.

207
00:15:49,940 --> 00:15:51,980
So let's add a maximum

208
00:15:53,990 --> 00:15:56,270
length of.

209
00:15:57,570 --> 00:16:05,060
The mill type is just a short one, so let's have it like 200 and for the price as well.

210
00:16:05,070 --> 00:16:06,690
Maximum digits.

211
00:16:07,680 --> 00:16:08,790
In here.

212
00:16:08,970 --> 00:16:11,790
Max digits.

213
00:16:13,610 --> 00:16:19,190
Let's set it to ten and yeah, let's run the command again.

214
00:16:19,400 --> 00:16:22,070
Python manage.py make migrations.

215
00:16:22,550 --> 00:16:24,740
This time it's successful.

216
00:16:24,740 --> 00:16:33,890
So we've created migrations for the modal item, so we still haven't actually altered the database.

217
00:16:33,890 --> 00:16:36,290
We haven't created a table yet.

218
00:16:36,320 --> 00:16:39,860
We have these files on the restaurant menu.

219
00:16:39,860 --> 00:16:47,590
These are the migrations files which now will be executed by the commands.

220
00:16:47,600 --> 00:16:51,650
Python manage.py migrate.

221
00:16:51,680 --> 00:16:55,720
This commands will go through this two files.

222
00:16:55,730 --> 00:17:00,830
It will execute the code of this file actually 0001.

223
00:17:01,550 --> 00:17:06,589
That was the file that was generated by the Make Migrations command.

224
00:17:06,619 --> 00:17:10,250
That file will now be executed by the migrate command.

225
00:17:10,250 --> 00:17:11,780
So execute that.

226
00:17:12,200 --> 00:17:19,130
And now a table should have been created in the DB dot SQLite three database.

227
00:17:19,140 --> 00:17:22,859
You cannot read that file by just clicking it.

228
00:17:22,890 --> 00:17:28,650
You need a program and one program you can use is DB browser for SQLite.

229
00:17:28,680 --> 00:17:34,470
So you can download it, install it and then go to open database.

230
00:17:34,500 --> 00:17:45,030
Locate your app app 18 in my case the project directory and then locate DB dot SQL three press enter

231
00:17:45,030 --> 00:17:50,280
to open it, go to browse, select the table you want to browse.

232
00:17:50,280 --> 00:17:53,130
So that's the restaurant menu item you see.

233
00:17:53,160 --> 00:17:58,860
Restaurant Restaurant menu is the app name and item is the name of the class.

234
00:17:58,860 --> 00:18:00,840
Write the modal.

235
00:18:01,800 --> 00:18:05,910
So we name this class item and that's the name here.

236
00:18:05,910 --> 00:18:07,650
So select that.

237
00:18:07,830 --> 00:18:13,410
And here you'll see that this is a table that has all these fields.

238
00:18:13,410 --> 00:18:19,500
So meal description, price, meal type status date created date updated and author ID.

239
00:18:21,910 --> 00:18:25,540
Among this, you will also see like the auth user.

240
00:18:25,550 --> 00:18:28,510
So that's the other table I was talking about.

241
00:18:29,710 --> 00:18:36,850
So this table is associated with this table using that user.

242
00:18:40,040 --> 00:18:42,680
Model defined in here?

243
00:18:44,090 --> 00:18:44,510
Yeah.

244
00:18:44,510 --> 00:18:48,650
So that's the database structure that was successful.

245
00:18:48,650 --> 00:18:57,200
And next, we are ready now to go to the next step of creating views for our models.

246
00:18:57,800 --> 00:18:59,980
So with that, I'll see you in the next video.

247
00:18:59,990 --> 00:19:00,410
Thanks.

