1
00:00:00,170 --> 00:00:01,100
Hey, welcome back.

2
00:00:01,130 --> 00:00:06,350
In the previous video, we set up a Django project and the Django app.

3
00:00:06,350 --> 00:00:11,180
So we've got the configuration files here in our project directory.

4
00:00:11,180 --> 00:00:15,050
And this is what we see so far on the front end.

5
00:00:15,140 --> 00:00:17,780
Now we want to develop the app.

6
00:00:17,780 --> 00:00:25,280
We want to add code to the app so that we can customize and build what we actually need, which is the

7
00:00:25,280 --> 00:00:26,990
job application form.

8
00:00:27,020 --> 00:00:28,250
How do we do that?

9
00:00:28,280 --> 00:00:33,680
Well, it's common with Django apps to start from the database.

10
00:00:33,680 --> 00:00:41,720
So a bottom up approach, meaning that you start with a database, the back end, you code the python

11
00:00:41,720 --> 00:00:50,060
files and then you code the front end, which is the HTML part and CSS and bootstrap.

12
00:00:50,060 --> 00:00:53,990
So a bottom up approach starting from the database.

13
00:00:53,990 --> 00:00:56,480
So let's create a database module.

14
00:00:56,480 --> 00:00:57,500
You'll see.

15
00:00:57,500 --> 00:01:02,580
I'll explain as we go through this video, I'll explain what a module is.

16
00:01:02,970 --> 00:01:05,640
So let's stop the server first.

17
00:01:05,640 --> 00:01:10,620
So to stop the Django app, you want to go to the terminal where the Django app is running.

18
00:01:10,620 --> 00:01:11,460
So here.

19
00:01:11,490 --> 00:01:17,670
So put your cursor at the end and press control C, You also see it here.

20
00:01:17,670 --> 00:01:19,080
So on your keyboard control.

21
00:01:19,110 --> 00:01:21,060
C And that will stop the server.

22
00:01:21,180 --> 00:01:30,720
Let's create a model to create a database model you want to go to job application so the app directory

23
00:01:30,720 --> 00:01:32,220
and go to models.

24
00:01:32,550 --> 00:01:41,640
Double click that Models.py was created when we ran the Python Manage.py start app command.

25
00:01:41,700 --> 00:01:44,760
So all these files, including Models.py.

26
00:01:45,240 --> 00:01:55,350
And so Django added this lines in here, but we need to delete this comment and create our model.

27
00:01:55,500 --> 00:02:03,450
So a model meaning a class which you can name whatever you like, but I do recommend to name it form.

28
00:02:03,450 --> 00:02:08,729
And this class should inherit from models dot model.

29
00:02:08,729 --> 00:02:11,610
So this was imported here models.

30
00:02:11,610 --> 00:02:16,110
And so this class is a child of the model class, right?

31
00:02:16,110 --> 00:02:24,660
So a model class now is specialized to specify the columns of.

32
00:02:27,250 --> 00:02:28,870
The database table.

33
00:02:28,870 --> 00:02:29,440
Right.

34
00:02:30,550 --> 00:02:36,430
So our database table will have several fields, several columns, as you know.

35
00:02:36,430 --> 00:02:38,770
So this will be the output at the end.

36
00:02:39,460 --> 00:02:44,140
So this is from the previous Flask app that we built in the previous sections.

37
00:02:44,140 --> 00:02:45,670
But the output will be the same.

38
00:02:45,670 --> 00:02:54,430
So we'll have a first name input box here, last name, email, available start date and current occupation.

39
00:02:54,430 --> 00:03:00,670
So when the user enters the data here, they fill in the data they press submit This data will be stored

40
00:03:00,670 --> 00:03:02,260
in a database table.

41
00:03:02,260 --> 00:03:02,740
Right?

42
00:03:02,740 --> 00:03:10,390
So that table should have these columns, first name, last name, email, date and occupation.

43
00:03:10,750 --> 00:03:14,560
So in here we are designing the database.

44
00:03:14,560 --> 00:03:17,080
That's what it is meant by model.

45
00:03:17,080 --> 00:03:26,010
The database model car fields means a character field with a maximum length of 80.

46
00:03:26,050 --> 00:03:34,280
Make a break line there and you want to duplicate this with Ctrl or command D and change it to last

47
00:03:34,280 --> 00:03:37,610
name, which is also the same type of field.

48
00:03:37,640 --> 00:03:41,450
So this field can accept 80 characters in total.

49
00:03:41,930 --> 00:03:51,470
And then we have the email, this can be an email field, so delete that.

50
00:03:52,280 --> 00:03:56,090
This is a specialized field to store email addresses.

51
00:03:56,930 --> 00:03:58,550
Then we have the date.

52
00:04:00,220 --> 00:04:05,260
This is also another specialized field date field.

53
00:04:06,430 --> 00:04:08,320
And lastly, occupation.

54
00:04:08,320 --> 00:04:11,500
This is just another care field.

55
00:04:12,400 --> 00:04:13,660
Modules that care.

56
00:04:14,680 --> 00:04:15,370
Fields.

57
00:04:15,520 --> 00:04:20,680
Max underscore length is again 80.

58
00:04:20,950 --> 00:04:28,540
And there's one more thing we need to add here, and that is a magic str methods underscore underscore

59
00:04:28,600 --> 00:04:31,600
str self as an argument.

60
00:04:31,630 --> 00:04:35,980
I'll explain all this what this method does return f.

61
00:04:37,090 --> 00:04:40,720
Self dot first name.

62
00:04:43,020 --> 00:04:45,690
So Dot lost.

63
00:04:46,610 --> 00:04:47,450
Name.

64
00:04:47,780 --> 00:04:49,160
What is str.

65
00:04:49,190 --> 00:04:49,490
Here?

66
00:04:49,490 --> 00:04:53,510
Well, let me quickly show you that you in a python console.

67
00:04:53,990 --> 00:04:57,980
So let's say we have a class foo, right?

68
00:05:00,080 --> 00:05:10,010
Now if Foo doesn't have a string method, when you create an instance of foo like this foo with lowercase

69
00:05:10,040 --> 00:05:17,690
f and you print that out, you print foo, you don't get any readable output.

70
00:05:17,690 --> 00:05:27,920
But if you have this class foo again and you have a method str and here you can define what this method

71
00:05:27,920 --> 00:05:28,730
will return.

72
00:05:28,730 --> 00:05:31,400
For example, it might return.

73
00:05:31,760 --> 00:05:35,340
Hello, I am foo right.

74
00:05:36,140 --> 00:05:44,870
And then if you create an instance again of this new foo class and you print foo, then you will get

75
00:05:44,870 --> 00:05:49,250
the string representation of that instance.

76
00:05:49,250 --> 00:05:49,730
Right?

77
00:05:49,730 --> 00:05:57,650
So this magic method defines what should be printed when a print function is used.

78
00:05:57,680 --> 00:06:05,730
Or in another case like in this case, this should print out the name of the user or the surname, or

79
00:06:05,730 --> 00:06:11,250
it will display it in the admin interface when we create an admin interface later on.

80
00:06:11,250 --> 00:06:20,520
So Django will will display this class, the instance of this class in the form of its salts, for example,

81
00:06:20,700 --> 00:06:22,470
or another user.

82
00:06:22,920 --> 00:06:24,510
I hope that's clear.

83
00:06:25,360 --> 00:06:25,810
Right.

84
00:06:25,810 --> 00:06:35,750
So now we have a model, but how do we actually apply this model to the database and where is the database?

85
00:06:35,770 --> 00:06:41,260
Well, the database should be somewhere in here in the project directory.

86
00:06:41,290 --> 00:06:43,660
DB dot SQLite three.

87
00:06:44,170 --> 00:06:44,830
Right.

88
00:06:45,310 --> 00:06:49,300
If you're curious to know what's inside that database.

89
00:06:49,300 --> 00:06:54,730
So if we have this table or not, I can actually show you that to you.

90
00:06:54,730 --> 00:06:59,410
Using the DB browser for SQLite program, you can install this program.

91
00:06:59,410 --> 00:07:07,520
You can search on Google DB browser for SQLite if you're curious, or you can just watch me as I demonstrate

92
00:07:07,540 --> 00:07:10,000
you what this database has inside.

93
00:07:10,000 --> 00:07:18,730
So I want to open the database using this button and I want to locate the Django project.

94
00:07:19,470 --> 00:07:20,340
So there we go.

95
00:07:20,340 --> 00:07:22,350
That's DB SQLite three.

96
00:07:22,380 --> 00:07:23,160
Open it.

97
00:07:23,160 --> 00:07:26,120
And now we don't have any tables.

98
00:07:26,130 --> 00:07:26,790
Right?

99
00:07:26,790 --> 00:07:31,370
But we are defining here a form table.

100
00:07:31,380 --> 00:07:34,540
So how comes that we don't have any tables here?

101
00:07:34,560 --> 00:07:39,450
Well, that's because we haven't applied any migrations.

102
00:07:39,480 --> 00:07:41,640
Database migrations yet.

103
00:07:41,640 --> 00:07:45,690
So what we want to do next is to create migrations.

104
00:07:45,720 --> 00:07:48,190
I'll explain what migrations are.

105
00:07:48,210 --> 00:07:57,030
So we want to go to the terminal and type in Python Manage.py make migrations, don't execute it yet.

106
00:07:57,060 --> 00:07:59,760
So what does this command do now?

107
00:07:59,790 --> 00:08:07,290
Well, if you go to the job application directory, you will find a migrations directory.

108
00:08:07,290 --> 00:08:11,520
If you don't have it, don't worry, it will be created when we run this command.

109
00:08:11,550 --> 00:08:18,510
So until this migrations directory I see this init.py file which is empty.

110
00:08:18,540 --> 00:08:21,550
Now when I run this command.

111
00:08:22,390 --> 00:08:25,400
Python manage.py make migrations.

112
00:08:25,480 --> 00:08:29,500
Some python code will be added to this init method.

113
00:08:29,620 --> 00:08:32,049
I'll explain what that python code is.

114
00:08:32,080 --> 00:08:38,530
First, let's run the command, but make sure that in settings settings which is.

115
00:08:39,669 --> 00:08:40,900
Under my side.

116
00:08:40,900 --> 00:08:44,950
So my side is the project in settings.py.

117
00:08:44,980 --> 00:08:50,500
You have job application added already under installed apps just like you see it here.

118
00:08:50,500 --> 00:08:55,480
So job application is the name of our app job underscore application.

119
00:08:55,480 --> 00:08:57,940
So close settings and run.

120
00:08:57,940 --> 00:09:01,210
This commands python manage.py make migrations.

121
00:09:01,570 --> 00:09:04,570
You should see this output in the command line.

122
00:09:04,570 --> 00:09:14,110
Now if you go back to job application migrations, you will see this empty file again and also this

123
00:09:14,110 --> 00:09:16,450
other file initial.py.

124
00:09:16,720 --> 00:09:25,480
So when we executed this command python manage.py make migrations, what happens is that the form class

125
00:09:25,510 --> 00:09:29,380
is translated into python code.

126
00:09:29,380 --> 00:09:30,700
Some other code.

127
00:09:30,700 --> 00:09:41,990
Right now this code here is like the middleman between the form class and the DB database.

128
00:09:41,990 --> 00:09:42,410
Right?

129
00:09:42,440 --> 00:09:44,270
DB dot sqlite three.

130
00:09:44,300 --> 00:09:48,920
So we still haven't touched the db dot sqlite database.

131
00:09:48,920 --> 00:09:54,230
So if I go back to DB browser, we still see no tables here.

132
00:09:54,230 --> 00:09:58,490
So nothing has been changed in the database.

133
00:09:58,910 --> 00:10:05,300
To apply those migrations we need to execute another command.

134
00:10:05,300 --> 00:10:08,990
Python manage.py migrate.

135
00:10:09,020 --> 00:10:11,180
So press enter.

136
00:10:11,300 --> 00:10:19,610
And now if I go back to DB browser, I want to load the database again to make a refresh.

137
00:10:19,610 --> 00:10:22,280
So db sqlite three open.

138
00:10:22,310 --> 00:10:24,920
Now we see 12 tables.

139
00:10:25,790 --> 00:10:26,630
You see.

140
00:10:27,360 --> 00:10:28,830
One of those tables.

141
00:10:28,830 --> 00:10:33,090
So database tables, one of those is job application form.

142
00:10:33,450 --> 00:10:36,390
And if I expand this, we should see.

143
00:10:37,270 --> 00:10:44,020
These fields, the columns that we created in Models.py.

144
00:10:44,350 --> 00:10:48,340
So to wrap it up, what we did is we created this class.

145
00:10:48,340 --> 00:10:56,980
So the design of our database, the blueprint, then we ran make migrations, Python Manage.py make

146
00:10:56,980 --> 00:11:03,190
migrations that generated this file with python code and this python code.

147
00:11:04,310 --> 00:11:14,990
Is then executed when we run python manage.py migrate and that python code alters the database, it

148
00:11:14,990 --> 00:11:16,490
adds all those tables.

149
00:11:16,490 --> 00:11:18,410
Now what are these other tables?

150
00:11:18,410 --> 00:11:23,480
Well, these are the tables that belong to the project, not the app itself.

151
00:11:23,480 --> 00:11:27,410
So job application form is a table that belongs to the app.

152
00:11:27,410 --> 00:11:29,720
But these were created because.

153
00:11:30,250 --> 00:11:32,980
We also have this project, the Django Project.

154
00:11:32,980 --> 00:11:37,870
So these are like tables which contain configurations about the project.

155
00:11:37,900 --> 00:11:43,570
Of course, if I go to browse data and I select the job application form table, there are no entries

156
00:11:43,570 --> 00:11:51,550
here, but these columns, these rows will be populated with data when we create the front end, the

157
00:11:51,550 --> 00:11:56,410
web page where the user can enter the data their entries.

158
00:11:56,410 --> 00:12:05,830
So one last thing is that if at a future point you want to add another field to this table, I don't

159
00:12:05,830 --> 00:12:07,480
recommend you do that now.

160
00:12:07,480 --> 00:12:13,060
So please don't do it now because you still don't have the proper skills to make such changes.

161
00:12:13,060 --> 00:12:20,620
But if later on you want to add a new field to your database table, for example, you could add a city

162
00:12:20,800 --> 00:12:23,590
field here in the form class.

163
00:12:23,590 --> 00:12:32,990
Then you would have to apply Python manage.py make migrations to create a new file here.

164
00:12:32,990 --> 00:12:37,670
So that's what creates a new.py file under the migrations folder.

165
00:12:37,700 --> 00:12:45,470
And then you'd have to run python manage.py migrate to apply those changes to the database table.

166
00:12:45,740 --> 00:12:46,590
Right.

167
00:12:46,610 --> 00:12:47,720
I hope that's clear.

168
00:12:47,720 --> 00:12:55,250
And yeah, that's about creating a database model and applying that model to the actual database.

169
00:12:55,250 --> 00:12:56,420
So thanks for following.

170
00:12:56,450 --> 00:12:57,230
See you in the next one.

