1
00:00:00,620 --> 00:00:01,954
- [Instructor] Hi, and welcome back.

2
00:00:01,954 --> 00:00:05,926
In this video, we're
going to be using Models.

3
00:00:05,926 --> 00:00:09,740
Now, we have a Resources
folder and a Models folder.

4
00:00:09,740 --> 00:00:11,760
Apologies, packages.

5
00:00:11,760 --> 00:00:13,472
And in the Resources package,

6
00:00:13,472 --> 00:00:17,247
we've got the Item file
which contains two classes,

7
00:00:17,247 --> 00:00:20,343
Item and Item list down here.

8
00:00:20,343 --> 00:00:22,164
And they're both resources

9
00:00:22,164 --> 00:00:24,639
which means they both belong in here,

10
00:00:24,639 --> 00:00:27,034
inside the Resources package.

11
00:00:27,034 --> 00:00:29,679
However, the User file contains a class

12
00:00:29,679 --> 00:00:34,262
which is not a resource as well
as one which is a resource.

13
00:00:34,262 --> 00:00:37,227
So, what we're gonna do is
we're going to move this class

14
00:00:37,227 --> 00:00:40,529
our of this file seeing as
it doesn't belong there,

15
00:00:40,529 --> 00:00:42,717
even though everything
works if it is there,

16
00:00:42,717 --> 00:00:45,694
but it just doesn't make sense
from a logical point of view,

17
00:00:45,694 --> 00:00:48,653
so what I've done is
I've cut that class out

18
00:00:48,653 --> 00:00:51,133
and we're going to
create a new file inside

19
00:00:51,133 --> 00:00:53,966
the models folder, call it User.py

20
00:00:55,169 --> 00:00:57,516
and paste that thing in there.

21
00:00:57,516 --> 00:01:00,391
So, that's our class which
we've essentially just moved

22
00:01:00,391 --> 00:01:05,221
from the resources package
to the models package.

23
00:01:05,221 --> 00:01:08,520
Now, we have to remember to
import the things that we need,

24
00:01:08,520 --> 00:01:12,002
which are gonna be sqlite3 only.

25
00:01:12,002 --> 00:01:15,497
So, I'm gonna save User.py in resources

26
00:01:15,497 --> 00:01:19,247
and go back to the models
and import sqlite3.

27
00:01:20,457 --> 00:01:23,072
Now, we need sqlite3
because the find_by_username

28
00:01:23,072 --> 00:01:27,239
and find_by_ID methods in
the User class use sqlite3.

29
00:01:29,543 --> 00:01:31,347
So, let me just take a moment to explain

30
00:01:31,347 --> 00:01:35,101
what a model is because
up until now we've thought

31
00:01:35,101 --> 00:01:39,268
of our resource as things
that the API responds with,

32
00:01:41,122 --> 00:01:45,289
things that API clients can
ask for, and things like that.

33
00:01:46,322 --> 00:01:49,989
And that is true, that's
what a resource is.

34
00:01:50,941 --> 00:01:55,108
And essentially a resource
is what the API thinks of.

35
00:01:56,536 --> 00:02:00,610
The API deals with
resources such as Users,

36
00:02:00,610 --> 00:02:02,610
items, stores, students,

37
00:02:03,750 --> 00:02:07,418
basically similar to object
oriented programming.

38
00:02:07,418 --> 00:02:11,459
So, our class User here is not a resource

39
00:02:11,459 --> 00:02:16,162
because the API cannot
receive data into this class

40
00:02:16,162 --> 00:02:19,829
or send this class as
a JSON representation,

41
00:02:20,742 --> 00:02:21,774
things like that.

42
00:02:21,774 --> 00:02:25,107
This class here is a helper essentially.

43
00:02:25,954 --> 00:02:29,790
It is a helper that we
use to store some data

44
00:02:29,790 --> 00:02:33,479
about the User and also a
helper that contains a couple

45
00:02:33,479 --> 00:02:36,605
of methods that allow
us to easily retrieve

46
00:02:36,605 --> 00:02:39,022
User objects from a database.

47
00:02:41,827 --> 00:02:43,005
So, that's what a model is.

48
00:02:43,005 --> 00:02:47,172
A model is our internal
representation of an entity.

49
00:02:48,736 --> 00:02:50,801
Whereas a resource is the external

50
00:02:50,801 --> 00:02:53,726
representation of an entity.

51
00:02:53,726 --> 00:02:57,151
Our API clients, like a
website or a mobile app,

52
00:02:57,151 --> 00:02:59,708
think they're interacting with resources,

53
00:02:59,708 --> 00:03:01,605
that's what they see.

54
00:03:01,605 --> 00:03:04,820
And when our API responds
it responds with resources.

55
00:03:04,820 --> 00:03:08,159
Again, that's what the client sees.

56
00:03:08,159 --> 00:03:12,326
However, when we deal internally
in our code with a User,

57
00:03:13,904 --> 00:03:16,882
for example in our security.py file,

58
00:03:16,882 --> 00:03:20,129
we are using find by Username for example.

59
00:03:20,129 --> 00:03:23,106
We're not using the resource,
we're using the model

60
00:03:23,106 --> 00:03:26,041
because that's what gives us the power

61
00:03:26,041 --> 00:03:29,049
and that's what has the code
that allows our programme

62
00:03:29,049 --> 00:03:31,100
to do what it has to do.

63
00:03:31,100 --> 00:03:34,459
So, the model essentially is a helper

64
00:03:34,459 --> 00:03:37,352
that allows our programme or
that gives us more flexibility

65
00:03:37,352 --> 00:03:41,677
in our programme without
polluting the resource,

66
00:03:41,677 --> 00:03:44,863
which is what the clients interact with.

67
00:03:44,863 --> 00:03:48,497
So, models and resources
are reasonably separate

68
00:03:48,497 --> 00:03:50,609
although they can be linked
in the case of our User

69
00:03:50,609 --> 00:03:53,492
register and our User model.

70
00:03:53,492 --> 00:03:56,585
If you go to the User
register, for example,

71
00:03:56,585 --> 00:03:59,495
we had the User.find_by_Username
method there,

72
00:03:59,495 --> 00:04:03,124
which means that this
resource is using the model

73
00:04:03,124 --> 00:04:05,207
and to get some data out.

74
00:04:06,873 --> 00:04:10,210
Okay, now that we've determined
the difference between

75
00:04:10,210 --> 00:04:13,431
a model, which is our
internal representation,

76
00:04:13,431 --> 00:04:16,360
and the resource, which is
the external representation,

77
00:04:16,360 --> 00:04:20,527
it makes sense to rename
the class to be UserModel

78
00:04:22,046 --> 00:04:23,379
instead of User.

79
00:04:24,272 --> 00:04:25,834
Okay?

80
00:04:25,834 --> 00:04:27,817
So, I'm going to copy the name

81
00:04:27,817 --> 00:04:30,072
because we're gonna be
changing this reference in

82
00:04:30,072 --> 00:04:31,713
a bunch of places.

83
00:04:31,713 --> 00:04:35,880
For example, in the user.py
file inside the Resources folder

84
00:04:37,969 --> 00:04:41,636
we're going to have to
import UserModel from

85
00:04:46,103 --> 00:04:47,770
the models.user file

86
00:04:49,378 --> 00:04:52,831
and then in any instance where
we used to use the user class

87
00:04:52,831 --> 00:04:56,201
now we have to change that to UserModel.

88
00:04:56,201 --> 00:04:58,096
Remember, we've not
changed any functionality,

89
00:04:58,096 --> 00:05:00,617
all we've done and once
again is moved things about

90
00:05:00,617 --> 00:05:03,826
to make things a little
easier to understand.

91
00:05:03,826 --> 00:05:08,251
Okay, so we used the User
file there and that's it.

92
00:05:08,251 --> 00:05:11,057
And if you wanna make
sure you can press Ctrl+F

93
00:05:11,057 --> 00:05:14,123
on Windows or Cmd+F on Mac

94
00:05:14,123 --> 00:05:16,889
and that in Atom allows you to find.

95
00:05:16,889 --> 00:05:19,715
And so just type User there
and then you'll be able to see

96
00:05:19,715 --> 00:05:23,048
that at no point the User class is used.

97
00:05:25,148 --> 00:05:28,942
So, then you press Escape
and that leaves that bit.

98
00:05:28,942 --> 00:05:31,323
So, in order to save this file

99
00:05:31,323 --> 00:05:35,128
and then also let's go
into the security.py file

100
00:05:35,128 --> 00:05:39,295
and change the input from
resources.user to models.user

101
00:05:40,293 --> 00:05:42,803
and from User to UserModel.

102
00:05:42,803 --> 00:05:44,872
And once again here use the User class,

103
00:05:44,872 --> 00:05:46,394
we're gonna change that.

104
00:05:46,394 --> 00:05:50,079
And down here we also use it
we're gonna change it, too.

105
00:05:50,079 --> 00:05:54,212
Remember, even though we've
renamed the class to UserModel

106
00:05:54,212 --> 00:05:56,601
the variables don't have to change.

107
00:05:56,601 --> 00:05:58,675
It's a UserModel class
that we're accessing,

108
00:05:58,675 --> 00:06:01,436
but really what it's
returning is a User entity.

109
00:06:01,436 --> 00:06:03,881
It just so happens that
it's the internal entity

110
00:06:03,881 --> 00:06:08,167
as opposed to the external
entity that we're dealing with.

111
00:06:08,167 --> 00:06:09,000
Okay?

112
00:06:10,237 --> 00:06:14,108
Ow that we've updated the
User to be a UserModel,

113
00:06:14,108 --> 00:06:16,418
let's do the same with the item.

114
00:06:16,418 --> 00:06:18,680
So, let's close these tabs here

115
00:06:18,680 --> 00:06:21,526
and create a new file
inside the Models folder

116
00:06:21,526 --> 00:06:23,193
and call it item.py.

117
00:06:24,044 --> 00:06:25,507
Remember you can call these
files whatever you want,

118
00:06:25,507 --> 00:06:27,695
it just makes sense
because there's going to be

119
00:06:27,695 --> 00:06:31,047
an item model to be in the models package

120
00:06:31,047 --> 00:06:32,109
and be called item.

121
00:06:32,109 --> 00:06:35,701
But if you think if you'd
rather call it item_model,

122
00:06:35,701 --> 00:06:38,346
for example, you can do that.

123
00:06:38,346 --> 00:06:42,513
Now, okay, so then what we
have to do here is not move

124
00:06:43,933 --> 00:06:46,895
because we don't have a
currently existing item model,

125
00:06:46,895 --> 00:06:50,765
but we're going to create an item model.

126
00:06:50,765 --> 00:06:53,398
Remember, this is the internal
representation if what

127
00:06:53,398 --> 00:06:55,731
an item does and looks like.

128
00:06:56,703 --> 00:06:58,550
So, let's go into the item resource

129
00:06:58,550 --> 00:07:02,717
and find out what things
we do in the item resource

130
00:07:03,578 --> 00:07:06,078
that could be done by a model.

131
00:07:07,716 --> 00:07:11,133
I immediately spot something that may not

132
00:07:12,549 --> 00:07:14,299
belong in a resource.

133
00:07:15,892 --> 00:07:20,052
Remember the resource
is used to map endpoints

134
00:07:20,052 --> 00:07:23,758
such as the get verb and the post verb

135
00:07:23,758 --> 00:07:27,425
and to the /item/name.

136
00:07:28,422 --> 00:07:32,005
And we've got a get,
post, delete, and put,

137
00:07:32,964 --> 00:07:35,930
but we also have a couple
of other methods here

138
00:07:35,930 --> 00:07:38,702
that maybe don't belong in our resource

139
00:07:38,702 --> 00:07:41,752
because the clients don't interact

140
00:07:41,752 --> 00:07:44,307
with these methods directly.

141
00:07:44,307 --> 00:07:47,974
An example of that is
this find_by_name method.

142
00:07:47,974 --> 00:07:51,807
And another example may
be this insert method.

143
00:07:53,043 --> 00:07:57,467
These two are not called
by an API directly,

144
00:07:57,467 --> 00:08:01,101
they are only used from within our code,

145
00:08:01,101 --> 00:08:03,745
so therefore it doesn't make
sense to pollute the resource

146
00:08:03,745 --> 00:08:07,421
with these methods because
it doesn't help at all.

147
00:08:07,421 --> 00:08:11,009
It makes more sense to move them elsewhere

148
00:08:11,009 --> 00:08:15,391
so that the resource is
only containing methods

149
00:08:15,391 --> 00:08:17,282
that the API interact with.

150
00:08:17,282 --> 00:08:18,401
Why?

151
00:08:18,401 --> 00:08:20,596
Just because it makes things
easier in the longterm

152
00:08:20,596 --> 00:08:22,586
for us to modify the code.

153
00:08:22,586 --> 00:08:26,172
If we go in here and we see
get, post, delete, and put,

154
00:08:26,172 --> 00:08:29,147
we know that anything we
change is gonna change our API.

155
00:08:29,147 --> 00:08:31,847
Whereas, if we change this insert method

156
00:08:31,847 --> 00:08:34,238
that's not gonna change our API,

157
00:08:34,238 --> 00:08:37,655
but it may modify the way our code works.

158
00:08:38,580 --> 00:08:39,615
Okay?

159
00:08:39,615 --> 00:08:43,581
So, let's go back into
item.py in the Models folder

160
00:08:43,581 --> 00:08:47,331
and let's import sqlite3
because we need that

161
00:08:48,729 --> 00:08:51,729
and then create the class ItemModel.

162
00:08:52,857 --> 00:08:56,784
Also remember the item model
is our internal representation,

163
00:08:56,784 --> 00:09:00,940
so it also has to contain
the properties of an item

164
00:09:00,940 --> 00:09:02,878
as object properties.

165
00:09:02,878 --> 00:09:06,024
So, for that we're going
to create an _init_ method

166
00:09:06,024 --> 00:09:08,661
and every item has a name and a price,

167
00:09:08,661 --> 00:09:10,974
so we're going to out
those in this argument

168
00:09:10,974 --> 00:09:15,141
and self.name = name, self.price
= price there as well.

169
00:09:17,114 --> 00:09:21,059
Okay, now I'm also going
to create a JSON method

170
00:09:21,059 --> 00:09:22,876
and all this is going to
do is it's going to return

171
00:09:22,876 --> 00:09:27,043
a JSON representation of the
model, basically a dictionary.

172
00:09:33,282 --> 00:09:34,317
Okay?

173
00:09:34,317 --> 00:09:38,146
So, that is a dictionary
representing our item.

174
00:09:38,146 --> 00:09:40,341
Every item contains a name and a price,

175
00:09:40,341 --> 00:09:43,729
so that's what our
dictionary contains, too.

176
00:09:43,729 --> 00:09:47,029
Now, the next steps are
to move all of the methods

177
00:09:47,029 --> 00:09:50,578
from the item resource
that don't belong there

178
00:09:50,578 --> 00:09:52,515
into the item model.

179
00:09:52,515 --> 00:09:54,088
Now, I'm sure that you can do that,

180
00:09:54,088 --> 00:09:57,073
I'd recommend you pause the
video and give that a go

181
00:09:57,073 --> 00:10:00,073
and then we'll do that here as well.

182
00:10:03,012 --> 00:10:04,795
Okay, hopefully you got that.

183
00:10:04,795 --> 00:10:07,145
I'm going into the item resource

184
00:10:07,145 --> 00:10:11,106
and I'm going to just cut
everything that doesn't belong.

185
00:10:11,106 --> 00:10:14,110
So, I'm going to cut
this class method there

186
00:10:14,110 --> 00:10:16,777
and I'm going to put it in here.

187
00:10:18,438 --> 00:10:20,883
And then I'm going to
cut this insert method

188
00:10:20,883 --> 00:10:25,650
that doesn't belong and
I'm going to put it here.

189
00:10:25,650 --> 00:10:28,453
Make sure the indentation
is always correct

190
00:10:28,453 --> 00:10:31,974
and so nothing is indented
more or less than it should be.

191
00:10:31,974 --> 00:10:33,887
And finally there is also
a cheeky method there

192
00:10:33,887 --> 00:10:36,408
that's also a bit hidden,
the update method,

193
00:10:36,408 --> 00:10:40,575
we're going to cut that and
put it down here as well, okay.

194
00:10:44,243 --> 00:10:47,383
Just as an aside, methods
within a class should be

195
00:10:47,383 --> 00:10:52,049
separated by a new line, so
as you can see this method

196
00:10:52,049 --> 00:10:54,555
and this method are
separated by a new line,

197
00:10:54,555 --> 00:10:57,075
but classes within a file
should be separated by two

198
00:10:57,075 --> 00:11:00,577
new lines, that's just sort
of a Python coding standard.

199
00:11:00,577 --> 00:11:02,476
No problem if you don't do that,

200
00:11:02,476 --> 00:11:05,701
but that's just the convention.

201
00:11:05,701 --> 00:11:09,993
Now, in all the places where
we used self.find_by_name

202
00:11:09,993 --> 00:11:13,324
we have to change that
to use the item model.

203
00:11:13,324 --> 00:11:16,459
So, let's go to the very top
and import the ItemModel.

204
00:11:16,459 --> 00:11:19,542
So, from models.item import ItemModel

205
00:11:21,206 --> 00:11:23,390
and then we're just gonna
change every instance

206
00:11:23,390 --> 00:11:27,473
of self for ItemModel
where appropriate remember.

207
00:11:30,378 --> 00:11:35,206
Okay, so we've got find_by_name,
find_by_name, insert,

208
00:11:35,206 --> 00:11:39,717
and then we have there's
delete method there,

209
00:11:39,717 --> 00:11:42,809
that's part of the API so nothing there.

210
00:11:42,809 --> 00:11:45,730
We've got self.find_by_name,
that's now in the ItemModel,

211
00:11:45,730 --> 00:11:48,063
self.insert and self.update.

212
00:11:49,605 --> 00:11:51,177
Okay?

213
00:11:51,177 --> 00:11:53,427
Which is quite interesting.

214
00:11:54,972 --> 00:11:56,168
So, what else?

215
00:11:56,168 --> 00:12:00,335
Well, now that the ItemModel
here is worried about

216
00:12:05,602 --> 00:12:07,673
essentially itself and
what I mean with that is

217
00:12:07,673 --> 00:12:11,840
this ItemModel is the item
that we are dealing with.

218
00:12:13,914 --> 00:12:17,638
So, we can create an new
ItemModel that stores

219
00:12:17,638 --> 00:12:19,221
a name and a price.

220
00:12:20,196 --> 00:12:21,971
And that creates an object that has

221
00:12:21,971 --> 00:12:24,221
those two properties in it.

222
00:12:26,045 --> 00:12:29,378
So, does it make sense for the ItemModel

223
00:12:31,858 --> 00:12:34,358
to have a class method insert?

224
00:12:36,897 --> 00:12:40,191
And now, what I mean with that is

225
00:12:40,191 --> 00:12:44,235
we have the ItemModel,
which represents an item,

226
00:12:44,235 --> 00:12:47,786
and this insert method
is taking in an item

227
00:12:47,786 --> 00:12:51,536
that it is going to
insert into the database.

228
00:12:54,106 --> 00:12:58,933
Would it not make sense for
this to not be a class method

229
00:12:58,933 --> 00:13:02,449
and for it just to use
self since essentially

230
00:13:02,449 --> 00:13:06,282
that's what it's doing,
it's inserting itself?

231
00:13:08,049 --> 00:13:10,978
Previously, for example,
in the resources file

232
00:13:10,978 --> 00:13:15,145
we would call this
itemModel.insert the updated_item,

233
00:13:16,472 --> 00:13:20,639
but it makes sense for us
to do updated_item.insert.

234
00:13:23,483 --> 00:13:27,650
And that that does is that
saves the item to the database.

235
00:13:30,207 --> 00:13:32,110
So, lets do just that.

236
00:13:32,110 --> 00:13:35,969
Let's go back into the model
and change the insert to model,

237
00:13:35,969 --> 00:13:37,868
the insert method to self,

238
00:13:37,868 --> 00:13:42,035
and the update method to
self as well, and of course,

239
00:13:43,296 --> 00:13:47,468
in the cursor we have to
make sure to use self.name.

240
00:13:47,468 --> 00:13:50,985
Remember, self is now an
object and not a dictionary,

241
00:13:50,985 --> 00:13:54,086
so we can access these
properties as we would in any

242
00:13:54,086 --> 00:13:55,836
other object as well.

243
00:14:06,130 --> 00:14:06,963
Okay.

244
00:14:08,110 --> 00:14:10,054
The find_by_name method, however,

245
00:14:10,054 --> 00:14:12,703
should still be a class method

246
00:14:12,703 --> 00:14:17,159
because it is gonna return
an object of ItemModel

247
00:14:17,159 --> 00:14:20,093
as opposed to a dictionary.

248
00:14:20,093 --> 00:14:23,419
So, what we're gonna do in
here is instead of returning

249
00:14:23,419 --> 00:14:25,487
a dictionary we're going to return

250
00:14:25,487 --> 00:14:28,339
an object of type ItemModel.

251
00:14:28,339 --> 00:14:31,190
This is a class method,
which means it has CLS,

252
00:14:31,190 --> 00:14:33,130
which is a reference to the class.

253
00:14:33,130 --> 00:14:36,756
So, can you modify this
code so that it returns

254
00:14:36,756 --> 00:14:40,923
an object of type ItemModel
instead of a dictionary?

255
00:14:46,944 --> 00:14:48,035
I'm sure you got it.

256
00:14:48,035 --> 00:14:51,368
All we have to do is make sure to do CLS

257
00:14:52,278 --> 00:14:54,528
and then row[0] and row[1].

258
00:14:55,833 --> 00:14:59,018
And once again what that does
is CLS calls the ItemModel

259
00:14:59,018 --> 00:15:01,513
in its method, row[0] Was the name,

260
00:15:01,513 --> 00:15:04,071
which goes into the name argument,

261
00:15:04,071 --> 00:15:07,045
row[1] Was the price , which
goes into the price argument

262
00:15:07,045 --> 00:15:10,462
and that gives us a new ItemModel object.

263
00:15:11,364 --> 00:15:15,996
Of course, row[0] also happens
to be the first parameter

264
00:15:15,996 --> 00:15:18,048
the parameter number zero,

265
00:15:18,048 --> 00:15:19,892
and row[1] also happens to be

266
00:15:19,892 --> 00:15:22,740
the second parameter number one.

267
00:15:22,740 --> 00:15:26,567
So, this is a perfect scenario
for us to use our argument

268
00:15:26,567 --> 00:15:28,067
unpacking like so.

269
00:15:30,528 --> 00:15:32,558
And once again, that does exactly the same

270
00:15:32,558 --> 00:15:35,862
it just passes each of the arguments

271
00:15:35,862 --> 00:15:39,258
or each of the elements
in the row to a parameter

272
00:15:39,258 --> 00:15:42,110
and delete method in turn.

273
00:15:42,110 --> 00:15:46,688
So, that just simplifies
the code just a bit more.

274
00:15:46,688 --> 00:15:50,195
Okay, so now that we've saved this,

275
00:15:50,195 --> 00:15:52,501
let's go back into our resources

276
00:15:52,501 --> 00:15:56,837
and now we're gonna make a
few changes here as well.

277
00:15:56,837 --> 00:16:00,254
Okay, remember the ItemModel.find_by_name

278
00:16:02,391 --> 00:16:06,558
now returns an item object
as opposed to a dictionary

279
00:16:08,681 --> 00:16:11,528
and that means that we cannot return item

280
00:16:11,528 --> 00:16:13,093
because that's an time object,

281
00:16:13,093 --> 00:16:15,522
an ItemModel object as
it was to a dictionary,

282
00:16:15,522 --> 00:16:18,939
so we have the return is item.json, okay.

283
00:16:28,559 --> 00:16:32,559
And what that's gonna
return is the item itself.

284
00:16:35,406 --> 00:16:38,342
Down here that's fine because
that's not returning anything.

285
00:16:38,342 --> 00:16:42,264
And here we've got an
item that is a dictionary.

286
00:16:42,264 --> 00:16:43,922
What we're gonna do is make sure that this

287
00:16:43,922 --> 00:16:47,957
is not a dictionary,
but an ItemModel object.

288
00:16:47,957 --> 00:16:51,485
So, what to do is just say ItemModel,

289
00:16:51,485 --> 00:16:53,402
name and price like so,

290
00:16:54,779 --> 00:16:56,636
and now that we've got an ItemModel

291
00:16:56,636 --> 00:16:59,106
this can be inserted into the database

292
00:16:59,106 --> 00:17:03,273
so we can change this thing
here to be item.insert.

293
00:17:04,672 --> 00:17:07,804
And that's just slightly easier

294
00:17:07,804 --> 00:17:12,297
because it doesn't
needlessly call the class

295
00:17:12,297 --> 00:17:14,547
instead of the item itself.

296
00:17:15,549 --> 00:17:19,716
Okay, down here once again
the time is a item object,

297
00:17:23,816 --> 00:17:26,978
the updated item is a item dictionary.

298
00:17:26,978 --> 00:17:29,723
So, what we have to do is
make sure that the object item

299
00:17:29,723 --> 00:17:32,140
is also an ItemModel like so.

300
00:17:39,346 --> 00:17:42,888
And then we have here
that if the item is a none

301
00:17:42,888 --> 00:17:45,487
means that we didn't find
an item in the database.

302
00:17:45,487 --> 00:17:47,976
We want to insert the updated item,

303
00:17:47,976 --> 00:17:50,643
so we can do updated_item.insert

304
00:17:53,204 --> 00:17:56,492
and if the item was none
that means that the,

305
00:17:56,492 --> 00:17:58,178
sorry if the item was not none,

306
00:17:58,178 --> 00:18:00,168
that means that the item was there.

307
00:18:00,168 --> 00:18:04,335
So, what we're going to do
is say updated_item.update.

308
00:18:07,668 --> 00:18:10,995
And remember, all that's
gonna do is return

309
00:18:10,995 --> 00:18:13,480
or rather update the database with

310
00:18:13,480 --> 00:18:15,647
the updated item's values.

311
00:18:16,769 --> 00:18:18,980
And finally, instead of
returning updated_item

312
00:18:18,980 --> 00:18:22,580
make sure to return updated_item.json.

313
00:18:22,580 --> 00:18:26,374
Now, I appreciate that
this looks a bit strange

314
00:18:26,374 --> 00:18:31,234
because the item is the one
we found in the database.

315
00:18:31,234 --> 00:18:33,484
Updated_item is a new item.

316
00:18:34,526 --> 00:18:38,431
So, surely we cannot
update the updated_item

317
00:18:38,431 --> 00:18:40,945
because that's not in the database,

318
00:18:40,945 --> 00:18:43,112
that item we just created.

319
00:18:43,946 --> 00:18:46,529
Surely we should do item.update

320
00:18:47,455 --> 00:18:51,917
so that we can update the
item with some new values.

321
00:18:51,917 --> 00:18:52,952
And I agree.

322
00:18:52,952 --> 00:18:55,981
I agree with you that this
is slightly more confusing

323
00:18:55,981 --> 00:18:57,586
than it should be.

324
00:18:57,586 --> 00:19:00,140
We're going to change
that in this section.

325
00:19:00,140 --> 00:19:03,257
However, remember we're
dealing with code here

326
00:19:03,257 --> 00:19:04,424
and not logic.

327
00:19:06,184 --> 00:19:10,303
The item is something we've
retrieved from the database,

328
00:19:10,303 --> 00:19:13,879
but this item isn't a row of a database,

329
00:19:13,879 --> 00:19:16,516
this item is an ItemModel.

330
00:19:16,516 --> 00:19:18,611
So, when we find it in the database,

331
00:19:18,611 --> 00:19:21,778
this item here is just a Python entity

332
00:19:22,926 --> 00:19:24,867
that contains a name and a price.

333
00:19:24,867 --> 00:19:26,284
That's all it is.

334
00:19:27,296 --> 00:19:30,845
This updated_item is another ItemModel,

335
00:19:30,845 --> 00:19:33,903
which contains a name,
presumably the same name,

336
00:19:33,903 --> 00:19:36,486
and possibly a different price.

337
00:19:38,675 --> 00:19:42,799
If we call item.update all
that's gonna do is it's going

338
00:19:42,799 --> 00:19:46,966
to go to the ItemModel
update with the item ask self

339
00:19:48,467 --> 00:19:51,973
and then it's going to
call this query here

340
00:19:51,973 --> 00:19:55,476
with the items.price and the items.name.

341
00:19:55,476 --> 00:19:57,878
Now, that's not going to change anything

342
00:19:57,878 --> 00:20:02,016
because that item is
already in the database.

343
00:20:02,016 --> 00:20:04,953
If we call updated_item.update,

344
00:20:04,953 --> 00:20:07,803
that doesn't make sense for us logically,

345
00:20:07,803 --> 00:20:10,773
but all that's going to
do is it's going to get

346
00:20:10,773 --> 00:20:15,031
the updated_items
properties in here itself

347
00:20:15,031 --> 00:20:17,847
and then it's going to
execute this equal query

348
00:20:17,847 --> 00:20:19,488
with that item's properties,

349
00:20:19,488 --> 00:20:22,605
which in turn is going to update the item

350
00:20:22,605 --> 00:20:25,331
that already is in the database.

351
00:20:25,331 --> 00:20:29,982
So, remember there is a big
disconnect between our database

352
00:20:29,982 --> 00:20:31,466
and our Python code.

353
00:20:31,466 --> 00:20:34,617
These two items here even
though one of them we retrieve

354
00:20:34,617 --> 00:20:37,131
from the database and the other we didn't,

355
00:20:37,131 --> 00:20:39,771
essentially act on the same row,

356
00:20:39,771 --> 00:20:42,242
which is very, very weird.

357
00:20:42,242 --> 00:20:44,022
And but, we're going to improve that,

358
00:20:44,022 --> 00:20:46,212
so don't worry about it too much

359
00:20:46,212 --> 00:20:50,945
and because we are going
to make things better.

360
00:20:50,945 --> 00:20:53,794
Okay, so this video
was gettin' pretty long

361
00:20:53,794 --> 00:20:56,409
and so I think that's
everything for this video.

362
00:20:56,409 --> 00:21:00,939
Let's make sure that we've not
missed any part of the code

363
00:21:00,939 --> 00:21:03,281
or like any missing imports
and things like that,

364
00:21:03,281 --> 00:21:05,930
but it's also very important.

365
00:21:05,930 --> 00:21:07,347
This is all fine.

366
00:21:09,402 --> 00:21:11,625
We've imported the ItemModel

367
00:21:11,625 --> 00:21:14,682
and we're using that everywhere

368
00:21:14,682 --> 00:21:17,649
and every item we are returning item.json

369
00:21:17,649 --> 00:21:21,851
as opposed to the item itself
except in the post method.

370
00:21:21,851 --> 00:21:25,985
So, this seems like an
important thing to do.

371
00:21:25,985 --> 00:21:28,424
And in the post method we
were returning just item

372
00:21:28,424 --> 00:21:30,605
as opposed to item.json

373
00:21:30,605 --> 00:21:33,400
and make sure that you're returning .json.

374
00:21:33,400 --> 00:21:35,131
If not, your API will not work

375
00:21:35,131 --> 00:21:37,637
because it cannot return item objects

376
00:21:37,637 --> 00:21:40,491
it can only return dictionaries.

377
00:21:40,491 --> 00:21:44,128
Okay, and this is not
returning anything, really.

378
00:21:44,128 --> 00:21:47,428
Well, it's returning a message,
not dealing with any items,

379
00:21:47,428 --> 00:21:49,164
so that's fine.

380
00:21:49,164 --> 00:21:51,976
And all of these things are fine as well

381
00:21:51,976 --> 00:21:54,612
and we are returning JSON there

382
00:21:54,612 --> 00:21:57,779
and the ItemList class it doesn't use,

383
00:21:59,199 --> 00:22:02,491
well we're not gonna change that just yet.

384
00:22:02,491 --> 00:22:04,560
We'll change that in the next video.

385
00:22:04,560 --> 00:22:06,825
And so that's all fine.

386
00:22:06,825 --> 00:22:11,357
And the User and resource as
well is using the UserModel

387
00:22:11,357 --> 00:22:14,524
where appropriate and that's all fine.

388
00:22:16,059 --> 00:22:18,441
Okay, so I think that's
everything for this video.

389
00:22:18,441 --> 00:22:21,284
And remember, it's really
important to check your code

390
00:22:21,284 --> 00:22:23,475
and read it over and over and over again

391
00:22:23,475 --> 00:22:26,787
and try it with Postman
every time that you make

392
00:22:26,787 --> 00:22:28,597
a few changes.

393
00:22:28,597 --> 00:22:33,347
While developing this
course I literally made tens

394
00:22:33,347 --> 00:22:36,563
of different versions incrementally

395
00:22:36,563 --> 00:22:38,753
and tested each one using Postman

396
00:22:38,753 --> 00:22:41,468
and just to make sure
that everything works

397
00:22:41,468 --> 00:22:44,773
every step of the way and
you should do that as well.

398
00:22:44,773 --> 00:22:45,796
As you're typing your code,

399
00:22:45,796 --> 00:22:47,259
always try it out with Postman,

400
00:22:47,259 --> 00:22:48,814
always look through and read it

401
00:22:48,814 --> 00:22:52,164
and re-read it until you
get so paranoid about making

402
00:22:52,164 --> 00:22:54,468
mistakes that you'll just
spot them immediately

403
00:22:54,468 --> 00:22:58,107
as you make them, but that's
part of software development,

404
00:22:58,107 --> 00:23:00,990
so hopefully you're okay
with signing up for that.

405
00:23:00,990 --> 00:23:02,728
And that's it for this video,

406
00:23:02,728 --> 00:23:05,223
so I'll see you on the very next one.

