1
00:00:05,230 --> 00:00:09,790
Welcome everyone to this lecture where we continue setting up our overall project with some models.

2
00:00:11,020 --> 00:00:15,400
So in order to continue setting up our library, we're going to need a model to actually understand

3
00:00:15,400 --> 00:00:16,120
what a book is.

4
00:00:16,420 --> 00:00:21,190
Things like the title of the book, the author, maybe a summary or ISBN number genre.

5
00:00:21,730 --> 00:00:27,450
And then speaking of that book and the genre, we can actually connect genres as its own model.

6
00:00:27,460 --> 00:00:31,600
So we can have genre as a model, and we can also have something like language as a model.

7
00:00:31,810 --> 00:00:36,040
So we're actually going to show you how to connect genre language and book models together.

8
00:00:36,580 --> 00:00:41,680
So essentially the genre of a book will actually be a reference to a genre model instance.

9
00:00:42,100 --> 00:00:44,870
Then we'll also have author be a model in itself.

10
00:00:44,890 --> 00:00:50,560
So, for example, while a book can have an author, the actual author object would be something more

11
00:00:50,560 --> 00:00:56,590
akin to a name, maybe a date of birth, date of death, books, etc. And then we can also have a book

12
00:00:56,590 --> 00:00:59,620
instance, which is a specific physical copy of the book.

13
00:01:00,010 --> 00:01:02,650
This might be the most confusing part, so let me hone in on it right now.

14
00:01:03,630 --> 00:01:08,340
Users of our library will eventually be able to check out a book instance, and we can have multiple

15
00:01:08,340 --> 00:01:09,870
book instances of the same book.

16
00:01:10,260 --> 00:01:14,730
For example, while you could have Catcher in the Rye be a book, you can have multiple physical book

17
00:01:14,730 --> 00:01:16,470
copies of Catcher in the Rye.

18
00:01:16,680 --> 00:01:19,530
That way, more than one person ends up checking out Catcher in the Rye.

19
00:01:19,710 --> 00:01:21,600
So those would be unique books.

20
00:01:21,600 --> 00:01:25,170
That is a unique instance of a physical Catcher in the Rye novel.

21
00:01:25,440 --> 00:01:28,740
So let's head over to our Voice Code editor and continue on.

22
00:01:32,730 --> 00:01:34,860
All right, so here I am back inside our project.

23
00:01:34,920 --> 00:01:39,510
I'm going to go to Models Dot Pi and begin to create our models.

24
00:01:39,540 --> 00:01:43,110
So this basically is just going to be typing in certain models that pi file.

25
00:01:43,530 --> 00:01:45,330
Be careful not to commit any typos here.

26
00:01:45,360 --> 00:01:46,800
You can always reference our code.

27
00:01:47,340 --> 00:01:53,100
I'm going to start off by actually creating the genre model first so you can see how we later connect

28
00:01:53,100 --> 00:01:54,600
this to the book model.

29
00:01:54,610 --> 00:01:56,760
And there's different ways of connecting models together.

30
00:01:57,060 --> 00:02:01,020
We can do something like a foreign key or even something like a many, too many.

31
00:02:01,230 --> 00:02:05,130
So first off, let's create a new class for this model called genre.

32
00:02:05,790 --> 00:02:08,039
And we inherit from models that model.

33
00:02:09,620 --> 00:02:15,320
And then I'm going to say something like this is a model representing a book genre, so we'll have an

34
00:02:15,320 --> 00:02:17,300
attribute for name of the genre.

35
00:02:18,590 --> 00:02:24,410
And then we're just going to say something like Models a character field and let's give the same max

36
00:02:24,410 --> 00:02:27,920
length of, let's say 150 characters.

37
00:02:28,670 --> 00:02:29,050
Okay.

38
00:02:29,060 --> 00:02:36,020
And then if you actually want to print out the genre, let's give it a string representation.

39
00:02:36,950 --> 00:02:39,830
So we'll say string peasant self here.

40
00:02:41,000 --> 00:02:43,940
And this will simply be something like return self taught name.

41
00:02:47,060 --> 00:02:47,360
Okay.

42
00:02:47,750 --> 00:02:54,110
So all we're doing here is setting up our class, genre and names equal to models, not character field.

43
00:02:54,590 --> 00:02:56,760
And right now you'll notice a lot of this text is white.

44
00:02:56,780 --> 00:03:00,760
So let me just quickly choose a different color scheme so we don't get confused.

45
00:03:02,690 --> 00:03:02,960
All right.

46
00:03:02,970 --> 00:03:03,320
There we go.

47
00:03:03,380 --> 00:03:08,150
Just switch the the color scheme that we hopefully a couple of things are different colors.

48
00:03:08,150 --> 00:03:12,770
So it's clear what's actually happening as far as what's a class call, what's a function call, what's

49
00:03:12,770 --> 00:03:13,790
a method, etc..

50
00:03:14,270 --> 00:03:15,560
So we have our genre class.

51
00:03:15,590 --> 00:03:17,360
Now let's set up our book model.

52
00:03:18,850 --> 00:03:20,530
And be careful again if you're indentation here.

53
00:03:21,040 --> 00:03:25,570
So the book model is essentially defining the single book title.

54
00:03:25,570 --> 00:03:26,330
You can think of it that way.

55
00:03:26,350 --> 00:03:30,610
Obviously, title is actually an attribute of a book, but this is not the book instance that we'll

56
00:03:30,610 --> 00:03:31,480
talk about later.

57
00:03:31,690 --> 00:03:32,710
So here we're going to say.

58
00:03:33,670 --> 00:03:35,320
Class book.

59
00:03:36,720 --> 00:03:37,800
Models that model.

60
00:03:40,490 --> 00:03:47,930
And here we're going to say title is equal to models and we'll have a character field for the actual

61
00:03:47,930 --> 00:03:48,530
book title.

62
00:03:48,530 --> 00:03:55,490
So I'm going to say like Maxlength is equal to take 200 characters doesn't really matter too much for

63
00:03:55,490 --> 00:03:59,780
our simple examples here next we want to do is have an author.

64
00:03:59,810 --> 00:04:03,420
Now I didn't mention later on we'll actually be creating an author model.

65
00:04:04,580 --> 00:04:09,950
So let's reference it here as a foreign key and then will actually build out that particular model so

66
00:04:09,950 --> 00:04:11,150
we can get the full reference.

67
00:04:11,270 --> 00:04:17,810
So essentially what I'm going to do is say that you have an author here, but I actually want book and

68
00:04:17,810 --> 00:04:19,279
author to be their own models.

69
00:04:19,279 --> 00:04:22,760
So this author is not just a simple character field.

70
00:04:22,850 --> 00:04:27,230
Instead, it's actually going to be linked to a specific author.

71
00:04:27,230 --> 00:04:28,910
So we're doing this story for key.

72
00:04:29,330 --> 00:04:30,170
Why do this?

73
00:04:30,200 --> 00:04:35,750
Well, an author could write multiple books, so be nice to have our own little database table of authors

74
00:04:35,750 --> 00:04:39,650
and then we can link them to the books that they have written that we later on, if we wanted to add

75
00:04:39,650 --> 00:04:44,660
more functionality, I could look up, Hey, this author and all the books they've written, sort of

76
00:04:44,660 --> 00:04:46,120
the same models, not for anarchy.

77
00:04:46,910 --> 00:04:48,320
And then we're going to say author.

78
00:04:49,820 --> 00:04:53,330
And then we can say a method on delete.

79
00:04:54,510 --> 00:04:58,710
Is equal to models that set null.

80
00:05:00,150 --> 00:05:06,600
And basically what this means is that you have to decide at a certain point when you start linking models.

81
00:05:06,780 --> 00:05:08,580
What happens if I delete this book?

82
00:05:08,790 --> 00:05:14,340
Would you also delete the author or would you delete the book if you happened to delete the author one

83
00:05:14,340 --> 00:05:15,030
way or the other?

84
00:05:15,390 --> 00:05:16,710
So there's different ways of doing this.

85
00:05:16,710 --> 00:05:19,480
And one way is to set models to set now.

86
00:05:19,500 --> 00:05:25,620
And essentially what happens here is that actually allows a database to store a null value if no author

87
00:05:25,620 --> 00:05:26,370
is selected.

88
00:05:26,820 --> 00:05:33,010
And that happens to mean that if you collect and delete two equal models that said no, that's actually

89
00:05:33,030 --> 00:05:37,290
a set the value of the books offer field to NULL if the associated author record is deleted.

90
00:05:37,710 --> 00:05:43,320
This is just a complicated way of saying that if for some reason your book exists but somebody x only

91
00:05:43,320 --> 00:05:44,790
deletes the author.

92
00:05:45,120 --> 00:05:47,000
It's not like the book suddenly disappears as well.

93
00:05:47,010 --> 00:05:53,010
You just are going to say, Hey, if you happen to odd delete the author, you will set it as null for

94
00:05:53,010 --> 00:05:53,550
this book.

95
00:05:53,670 --> 00:05:55,740
So it's kind of like the English translation of this code.

96
00:05:56,100 --> 00:06:03,060
So again, for the author on deletion of the author for this particular model, you'll set the author

97
00:06:03,060 --> 00:06:04,420
as null for the book.

98
00:06:04,680 --> 00:06:06,120
So hopefully that clarifies it.

99
00:06:06,480 --> 00:06:12,090
There's also another default behavior, which is Cascade, which means that if the author is deleted,

100
00:06:12,090 --> 00:06:13,380
the book would be deleted too.

101
00:06:13,410 --> 00:06:16,400
And sometimes maybe depending on the situation, you want that.

102
00:06:16,410 --> 00:06:20,700
So obviously there's something going on if you want to delete this author and suddenly delete all the

103
00:06:20,700 --> 00:06:23,220
books along with them because maybe it was a mistake.

104
00:06:23,250 --> 00:06:28,920
Then you may want to actually cascade that down so that actually end up being a delete is equal to models

105
00:06:28,920 --> 00:06:29,760
that cascade.

106
00:06:29,790 --> 00:06:32,580
But setting all is probably more appropriate for what we're doing here.

107
00:06:32,820 --> 00:06:36,930
We'll continue to build out book, but since I didn't mention that we're connecting this to an author

108
00:06:36,930 --> 00:06:39,330
model, let's quickly jump to the author model.

109
00:06:40,080 --> 00:06:44,160
So keep in mind more to come here for book, but let's set up this author model.

110
00:06:47,360 --> 00:06:48,530
Or C class author.

111
00:06:49,930 --> 00:06:50,800
Models not model.

112
00:06:51,790 --> 00:06:54,160
And then let's give them a first name.

113
00:06:55,060 --> 00:06:56,860
So models the character field.

114
00:06:58,050 --> 00:07:00,120
Let's just say 200 for the max length.

115
00:07:00,690 --> 00:07:03,630
Let's give them a last name as equal to models.

116
00:07:03,630 --> 00:07:05,850
The character field 200.

117
00:07:06,540 --> 00:07:09,120
And then, let's say date of birth.

118
00:07:11,220 --> 00:07:16,800
Date of birth is equal to models, and this probably makes more sense to be an actual date field.

119
00:07:17,430 --> 00:07:20,530
So have that be a date field and we'll go ahead and say NULL.

120
00:07:20,550 --> 00:07:24,870
It can be true if you don't happen to know them and will even let them leave it blank if we want to

121
00:07:25,140 --> 00:07:26,640
say blank is equal to true as well.

122
00:07:27,600 --> 00:07:27,900
Okay.

123
00:07:28,080 --> 00:07:31,110
So you could keep adding more information from on to like date of death, etc..

124
00:07:31,510 --> 00:07:37,320
Something we haven't seen before is adding a little bit of class here that can help us dictate behavior

125
00:07:37,320 --> 00:07:38,210
inside the admin view.

126
00:07:38,220 --> 00:07:40,970
Like what you want these sorted by or ordered by.

127
00:07:40,980 --> 00:07:43,230
So I can say ordering is equal to.

128
00:07:43,230 --> 00:07:47,850
And then you can have a list of how you want these ordered when you actually visit this or request this

129
00:07:47,850 --> 00:07:48,270
back.

130
00:07:48,750 --> 00:07:51,300
So I can say I want these ordered by last name.

131
00:07:52,430 --> 00:07:52,960
Karma.

132
00:07:53,240 --> 00:07:53,900
First name.

133
00:07:55,570 --> 00:07:55,930
Okay.

134
00:07:56,980 --> 00:08:02,550
And then finally, we're going to create some method calls here to actually define some URLs.

135
00:08:02,560 --> 00:08:06,520
So essentially it would return the URL to access a particular author instance.

136
00:08:06,700 --> 00:08:13,120
So the way this works is you create a method here called get absolute URL and you'll notice it actually

137
00:08:13,120 --> 00:08:13,960
autofill for me.

138
00:08:14,350 --> 00:08:16,840
But it's essentially going to be a reverse call.

139
00:08:17,440 --> 00:08:21,740
And instead of model detail, I switch this to say author detail.

140
00:08:21,910 --> 00:08:24,370
So I'm just switching for the actual name of the model.

141
00:08:24,820 --> 00:08:28,350
And then the keyword arguments is going to be here.

142
00:08:28,360 --> 00:08:29,130
You could say peak.

143
00:08:29,590 --> 00:08:31,990
Peak is equal to self peak.

144
00:08:33,010 --> 00:08:38,559
And I should point out that this does require a reverse call, which means up here I need to import

145
00:08:38,559 --> 00:08:44,950
that an essay from Django thought URLs imports reverse and we'll see that more later.

146
00:08:45,250 --> 00:08:49,540
But it's essentially they're saying, where's the URL that I could actually grab this author's detail

147
00:08:49,540 --> 00:08:49,850
from?

148
00:08:50,500 --> 00:08:53,300
So we have our author class.

149
00:08:53,320 --> 00:08:57,880
Last thing I want to actually mention here is how to get the string representation.

150
00:08:57,880 --> 00:09:02,650
So say str here passing self and we'll just return.

151
00:09:03,070 --> 00:09:04,810
You can use f string literal formatting.

152
00:09:05,230 --> 00:09:06,340
I'm going to return the last name.

153
00:09:06,340 --> 00:09:07,960
First name so I'll say last name.

154
00:09:08,200 --> 00:09:09,250
This should be self last name.

155
00:09:10,440 --> 00:09:11,400
So if last name.

156
00:09:12,630 --> 00:09:13,190
Karma.

157
00:09:14,450 --> 00:09:15,920
Self first name.

158
00:09:18,640 --> 00:09:19,420
And put that in.

159
00:09:19,700 --> 00:09:19,960
Okay.

160
00:09:19,960 --> 00:09:24,580
So that just returns, you know, something like last name, comma, first name, if you ask to actually

161
00:09:24,580 --> 00:09:25,480
print out that author.

162
00:09:26,080 --> 00:09:27,910
So, so far, so good for the author.

163
00:09:28,180 --> 00:09:30,340
You could keep adding more features if you wanted to.

164
00:09:30,610 --> 00:09:33,040
But let's head back now to book.

165
00:09:34,100 --> 00:09:34,570
Here.

166
00:09:34,640 --> 00:09:37,580
And now that we've set up author, we can see what's actually happening here.

167
00:09:37,940 --> 00:09:40,120
So if I delete this, Arthur, this book survives.

168
00:09:40,130 --> 00:09:42,140
It just sets the author to know.

169
00:09:42,770 --> 00:09:43,680
Now let's continue.

170
00:09:43,700 --> 00:09:44,990
What else do we want for the book?

171
00:09:45,710 --> 00:09:47,800
Maybe you want something like a summary.

172
00:09:47,810 --> 00:09:50,270
So, like a it's a description of the book.

173
00:09:50,870 --> 00:09:52,880
So let's go ahead and say summary of the book.

174
00:09:53,930 --> 00:09:54,770
Equals models.

175
00:09:54,800 --> 00:09:57,200
This probably didn't make sense as a character field because it's longer.

176
00:09:57,200 --> 00:09:59,990
So we'll say something like, What's a text field?

177
00:10:01,090 --> 00:10:06,970
And let's give it a longer maxlength like let's say that 600 or something with 600 characters, maybe

178
00:10:06,970 --> 00:10:08,770
that's too little, but not a big deal.

179
00:10:09,220 --> 00:10:14,530
And then if you want, you can also set this to be nulls equal to true or whatever, but we'll leave

180
00:10:14,530 --> 00:10:15,490
all the defaults there.

181
00:10:15,880 --> 00:10:18,640
So we have a title and author, a possible summary.

182
00:10:19,060 --> 00:10:21,610
Then let's go ahead and give this an ISBN number.

183
00:10:22,210 --> 00:10:24,940
So if you're checking out a book for the library, you know what I'm talking about here.

184
00:10:25,240 --> 00:10:29,470
It's not just Google Search, one ISBN number, essentially unique identifier for this book.

185
00:10:30,160 --> 00:10:32,860
So say character field and this is just going to be.

186
00:10:34,110 --> 00:10:35,050
ESPN.

187
00:10:35,730 --> 00:10:37,200
And let's give this a.

188
00:10:38,250 --> 00:10:44,190
Max length of I believe officially it's only 13 characters long and this one does have to be unique.

189
00:10:44,200 --> 00:10:49,650
We can't have multiple books of the same ISBN and if you're not familiar Price mean it's essentially

190
00:10:49,650 --> 00:10:52,080
a unique code to identify the books in the world.

191
00:10:52,470 --> 00:10:54,150
If you've heard of the Dewey Decimal System.

192
00:10:54,720 --> 00:10:54,990
All right.

193
00:10:56,060 --> 00:11:01,460
The last thing I want to do is actually connect this book to a genre.

194
00:11:02,000 --> 00:11:08,540
But unlike our simple model, where a book only has one unique author, technically there are cases

195
00:11:08,540 --> 00:11:11,270
in the real world where a book could have multiple authors.

196
00:11:11,270 --> 00:11:16,250
But in this case, we're just considering a book having one single unique author for genre.

197
00:11:16,280 --> 00:11:19,280
Let's show you how to connect multiple genres to a book.

198
00:11:19,580 --> 00:11:22,460
Maybe a book is both mystery and sci fi genre.

199
00:11:22,790 --> 00:11:28,190
So the way we do that is we actually classify this as a many to many connection, essentially saying

200
00:11:28,550 --> 00:11:31,140
that many books could have many genres.

201
00:11:31,160 --> 00:11:35,570
So for example, again, a book could be both a sci fi and a mystery novel.

202
00:11:35,630 --> 00:11:37,400
So let's go ahead and set that up.

203
00:11:37,790 --> 00:11:41,000
What I'm going to do here is set this up as a many, many fields.

204
00:11:41,000 --> 00:11:42,920
So we'll come here and say genre.

205
00:11:44,440 --> 00:11:52,540
Attribute is equal to models not and they'll say many to many field and here we connected with the genre

206
00:11:52,540 --> 00:11:57,220
class we created earlier and that's how you can connect it so that a book can then have essentially

207
00:11:57,220 --> 00:11:58,870
a list of potential genres.

208
00:11:59,410 --> 00:12:01,990
Let's also give a string representation for our book.

209
00:12:02,230 --> 00:12:03,040
I'll say def.

210
00:12:04,110 --> 00:12:05,100
That's tr self.

211
00:12:06,690 --> 00:12:09,540
And let's say it's going to return Self's title.

212
00:12:09,660 --> 00:12:11,040
It's by the simplest way of doing this.

213
00:12:11,880 --> 00:12:14,240
And let's also have it get absolute URL.

214
00:12:14,580 --> 00:12:18,380
It's going to return the URL to access a record for this book.

215
00:12:18,390 --> 00:12:22,320
So we'll say def get absolute URL.

216
00:12:23,440 --> 00:12:23,890
Self.

217
00:12:24,340 --> 00:12:26,380
And again, it's sort of model detail.

218
00:12:26,380 --> 00:12:27,810
I will say this is book detail.

219
00:12:27,820 --> 00:12:28,840
Notice I'm just using tab here.

220
00:12:28,840 --> 00:12:29,560
It's auto complete.

221
00:12:30,220 --> 00:12:34,960
So this is book detail and then we can have the identity be that.

222
00:12:35,320 --> 00:12:36,610
All right, so that's looking good.

223
00:12:37,150 --> 00:12:40,220
So next we have the actual book instance.

224
00:12:40,240 --> 00:12:44,200
So I have a book, a genre and then an author.

225
00:12:45,010 --> 00:12:47,350
And that's what I'm going to do is set up a book instance model.

226
00:12:47,710 --> 00:12:50,050
Remember, this one is actually going to be unique.

227
00:12:50,200 --> 00:12:56,740
So in that case, what I'm going to do and I'll do it here for now, I put this at the top is have you

228
00:12:56,740 --> 00:12:57,400
you ID.

229
00:12:58,730 --> 00:13:01,190
Which is a unique identifier generator.

230
00:13:01,200 --> 00:13:03,650
So essentially it will just generate a unique ID.

231
00:13:04,250 --> 00:13:05,780
You'll see why we do this in just a set.

232
00:13:06,350 --> 00:13:07,910
So we're going to say class.

233
00:13:08,950 --> 00:13:10,120
Book instance.

234
00:13:11,530 --> 00:13:12,430
Instance.

235
00:13:13,610 --> 00:13:15,140
L.S. models that model.

236
00:13:17,150 --> 00:13:26,240
And then from here, we're actually going to specify an ID attribute that is model's dot u u IED field.

237
00:13:26,900 --> 00:13:27,290
Okay.

238
00:13:27,320 --> 00:13:32,420
So essentially what's happening here is the book instance represents a specific copy of a book that

239
00:13:32,420 --> 00:13:37,370
someone might borrow and includes information maybe about whether the copies available or what date

240
00:13:37,370 --> 00:13:38,650
it's actually expected back.

241
00:13:38,660 --> 00:13:42,410
So we should know, hey, this particular book is actually checked out for the library.

242
00:13:42,890 --> 00:13:44,970
So some of the fields we're actually going to be familiar with.

243
00:13:44,990 --> 00:13:49,400
One of them, however, needs to be a representation of a specific copy.

244
00:13:49,700 --> 00:13:53,480
So for that, we can just essentially borrow this from the UUID Field Library.

245
00:13:54,020 --> 00:13:56,480
So we'll say this is a primary key is true.

246
00:13:57,870 --> 00:14:04,660
And then we're going to say default is equal to and this is what we use that import you, you ID, you

247
00:14:04,770 --> 00:14:05,500
id4.

248
00:14:06,420 --> 00:14:06,780
Okay.

249
00:14:07,870 --> 00:14:08,070
Oops.

250
00:14:08,200 --> 00:14:10,440
Make sure you do it.

251
00:14:10,870 --> 00:14:11,350
There we go.

252
00:14:12,300 --> 00:14:13,350
Since we imported that.

253
00:14:14,540 --> 00:14:16,640
And then we'll connect this to a book.

254
00:14:16,700 --> 00:14:19,940
So say book is equal to models for key.

255
00:14:20,240 --> 00:14:25,580
Kind of makes sense that a book instance should have a unique foreign key relationship to a single book.

256
00:14:27,080 --> 00:14:29,140
And then we're going to say on delete.

257
00:14:30,210 --> 00:14:34,020
So what happens when you actually do a deletion of a book?

258
00:14:34,590 --> 00:14:38,040
In this case, maybe you want to do something like a restrict call.

259
00:14:39,840 --> 00:14:43,230
This essentially restricts you from deleting a book if you still have a book instead.

260
00:14:43,250 --> 00:14:46,980
So you have to delete all the book instances first before you're allowed to delete that book.

261
00:14:47,010 --> 00:14:53,430
So that means I'm going to say all delete is equal to model's dot restrict, essentially saying, hey,

262
00:14:53,730 --> 00:14:59,220
if you have a book in the library or checked out and you try to just delete Catcher in the Rye, you

263
00:14:59,490 --> 00:15:02,070
have to actually take care of all those book instances first.

264
00:15:02,070 --> 00:15:05,780
You have to pull those out first before you can do a general book from your database.

265
00:15:05,790 --> 00:15:11,160
So we're going to restrict that, and then I'm going to say, no, all is equal to true here.

266
00:15:12,910 --> 00:15:14,710
And then I think that should be enough.

267
00:15:15,100 --> 00:15:17,350
Maybe we want something like an imprint on the book.

268
00:15:18,600 --> 00:15:19,680
Let's say imprints.

269
00:15:20,850 --> 00:15:24,000
Attribute is equal to models, not character field.

270
00:15:25,210 --> 00:15:26,350
Let's give it a max length.

271
00:15:26,740 --> 00:15:30,070
Let's say 200 such essentially can imprint something out of the book.

272
00:15:30,490 --> 00:15:32,470
And then more importantly, what does it do back?

273
00:15:34,140 --> 00:15:37,350
So this makes sense for it to be a field or say, the field.

274
00:15:38,650 --> 00:15:39,720
No is equal to true.

275
00:15:39,730 --> 00:15:41,620
So if it's null, that means it's in the library.

276
00:15:42,400 --> 00:15:44,980
And also, let it be blank if we just want to leave it blank.

277
00:15:45,920 --> 00:15:46,280
Okay.

278
00:15:47,060 --> 00:15:51,670
So we can have an attribute actually be a selection of something.

279
00:15:51,680 --> 00:15:56,480
So you can say, hey, what's the actual loan status for a particular book instance?

280
00:15:56,630 --> 00:15:57,500
How would we do that?

281
00:15:57,920 --> 00:16:01,580
Well, I can create an attribute that is essentially a tuple here.

282
00:16:01,970 --> 00:16:08,870
So for example, I can say loan status is equal to a couple of tuples where I'm going to have a parent

283
00:16:08,870 --> 00:16:09,170
here.

284
00:16:09,470 --> 00:16:11,420
We're going to say M for maintenance.

285
00:16:15,160 --> 00:16:17,680
Then we're going to say, Oh.

286
00:16:18,560 --> 00:16:19,280
For.

287
00:16:20,790 --> 00:16:21,480
On loan.

288
00:16:23,670 --> 00:16:25,620
We're going to say if it's available.

289
00:16:28,920 --> 00:16:30,690
And then I'm going to say are.

290
00:16:32,120 --> 00:16:33,740
If it's reserved.

291
00:16:36,290 --> 00:16:41,600
And then finally, let's now that we have this lone Sarasate tribute, we can use that to actually create

292
00:16:41,720 --> 00:16:43,370
a general status, that tribute.

293
00:16:45,250 --> 00:16:50,230
So say this is the model's character field and this will show you how to construct a field based off

294
00:16:50,230 --> 00:16:51,040
other attributes.

295
00:16:51,460 --> 00:16:57,640
So, for example, the max length of this field is just going to be one y1 because we're basing it off

296
00:16:57,640 --> 00:16:59,040
m, o or r.

297
00:16:59,620 --> 00:17:01,300
So the max length is one.

298
00:17:02,750 --> 00:17:03,710
The choices.

299
00:17:04,280 --> 00:17:04,910
Sorry about that.

300
00:17:05,390 --> 00:17:06,380
The choices here.

301
00:17:07,300 --> 00:17:10,210
Are going to be your loan status, are your choices.

302
00:17:11,500 --> 00:17:13,119
It can be blank if need be.

303
00:17:14,930 --> 00:17:17,930
The default behavior will be maintenance.

304
00:17:19,190 --> 00:17:20,450
So say default is M.

305
00:17:21,910 --> 00:17:23,290
And that is your status.

306
00:17:25,150 --> 00:17:26,950
Let's put that all in one line there so you can see it.

307
00:17:27,700 --> 00:17:28,210
There we go.

308
00:17:28,600 --> 00:17:30,290
So that's the status of the book.

309
00:17:30,310 --> 00:17:33,610
And then finally, if you want, we can set up a meta class for the ordering.

310
00:17:34,820 --> 00:17:38,480
So Saint Mata ordering here is when it's due back.

311
00:17:41,080 --> 00:17:41,980
What's the mixture?

312
00:17:41,980 --> 00:17:43,050
This actually matches here?

313
00:17:43,270 --> 00:17:43,780
Do back.

314
00:17:43,960 --> 00:17:44,650
Okay, perfect.

315
00:17:45,280 --> 00:17:51,550
And let's actually create an instance representation of how to actually print out a book here self.

316
00:17:52,960 --> 00:17:54,130
And then I was going to return.

317
00:17:54,190 --> 00:17:54,940
Let's see if.

318
00:17:56,260 --> 00:17:56,770
Died.

319
00:17:58,130 --> 00:18:02,780
And then it's probably also applicable to print out the actual book title itself.

320
00:18:02,790 --> 00:18:04,260
So it's that title.

321
00:18:04,640 --> 00:18:09,290
So I can't just say self that title because technically book instances don't have titles.

322
00:18:09,500 --> 00:18:15,020
However, book instances do have a book and the book is a foreign key, which in turn actually has the

323
00:18:15,020 --> 00:18:15,410
title.

324
00:18:15,440 --> 00:18:19,640
So the way I would reference that is I would say soft dot and then I would say self.

325
00:18:19,640 --> 00:18:21,800
That book, the self, that title.

326
00:18:24,540 --> 00:18:24,900
Okay.

327
00:18:24,930 --> 00:18:28,710
And that's how you can actually grab the title based off a foreign key relationship.

328
00:18:28,740 --> 00:18:32,040
So again, it's going to say this book instance is linked to a book.

329
00:18:32,340 --> 00:18:36,330
Then grab that book's title in order to print it out.

330
00:18:36,360 --> 00:18:40,230
If you ask for a book instance because we don't just want the idea, it's just going to be a random

331
00:18:40,230 --> 00:18:41,220
number that we won't recognize.

332
00:18:41,220 --> 00:18:43,560
It'd be nice to actually see the book title there just in case.

333
00:18:44,280 --> 00:18:48,660
Now, I did mention maybe you want a model for something like language.

334
00:18:48,690 --> 00:18:53,850
So let's imagine we're starting to expand the library and somebody donates a book in Spanish and now

335
00:18:53,850 --> 00:18:57,240
we have the same books, but now they're actually in different languages.

336
00:18:57,270 --> 00:18:58,140
So how do we do that?

337
00:18:58,530 --> 00:19:03,480
It probably makes sense to actually set up a language class and it would kind of operate similar to

338
00:19:03,480 --> 00:19:05,670
genre, but we would later on add it to a book.

339
00:19:05,820 --> 00:19:06,780
So let me show you.

340
00:19:07,830 --> 00:19:08,790
What this would look like.

341
00:19:09,000 --> 00:19:10,080
So I'll say class.

342
00:19:11,290 --> 00:19:14,230
And I'm going to now make a new class called My Language Class.

343
00:19:15,590 --> 00:19:16,550
Models that model.

344
00:19:17,810 --> 00:19:18,770
And it's gonna be pretty simple.

345
00:19:18,770 --> 00:19:19,490
She's going to be a name.

346
00:19:20,710 --> 00:19:22,960
So the name of the language should be a character field.

347
00:19:23,140 --> 00:19:25,810
Let's say Maxlength is equal to 200.

348
00:19:25,810 --> 00:19:33,100
So this is something I would say like Spanish, French, Japanese, etc. and then can be a simple string

349
00:19:33,100 --> 00:19:37,240
representation here to just return self thought the name of the language.

350
00:19:37,690 --> 00:19:42,550
Okay, the question becomes what's the best way to connect this language to our present book model?

351
00:19:42,760 --> 00:19:46,910
Well, in this case, a book is probably just in a single language.

352
00:19:46,930 --> 00:19:51,910
It wouldn't make sense to make it exactly like genre, because typically when you're thinking of the

353
00:19:51,910 --> 00:19:55,360
language of a book, it's not going to be both in English and Japanese.

354
00:19:55,420 --> 00:19:59,770
Now, maybe there's actually books on learning a language, which would be tricky, but in that case,

355
00:19:59,770 --> 00:20:02,330
you'd probably want to have it in the learner's language.

356
00:20:02,350 --> 00:20:07,300
So that is to say, if you have a book that teaches an English speaker how to speak Japanese, the language

357
00:20:07,300 --> 00:20:10,780
of that book should probably just be English, not both English and Japanese.

358
00:20:11,200 --> 00:20:14,350
So in that case, we would just come here and make it a foreign key.

359
00:20:15,220 --> 00:20:17,440
So I would say language and helps.

360
00:20:17,530 --> 00:20:18,490
That should be lowercase.

361
00:20:19,300 --> 00:20:23,290
The language of the book is equal to models that foreign key.

362
00:20:24,400 --> 00:20:26,440
And then I would think it up to language.

363
00:20:27,880 --> 00:20:28,390
There we go.

364
00:20:28,840 --> 00:20:32,680
So I would have each book now having some sort of language.

365
00:20:32,980 --> 00:20:36,010
Now the question is, what do we do if we were to delete a language?

366
00:20:36,400 --> 00:20:43,510
So oddly, plie makes more sense to just set this as the also, I'll say models that set null and we'll

367
00:20:43,510 --> 00:20:43,990
say null.

368
00:20:43,990 --> 00:20:44,770
That can be true.

369
00:20:45,820 --> 00:20:46,120
Okay.

370
00:20:46,390 --> 00:20:49,300
So that's how you could add in a new field for the book.

371
00:20:49,840 --> 00:20:53,980
So save all of that and let's go ahead and run some migrations.

372
00:20:54,010 --> 00:20:57,040
Remember, we have to first make the migrations and then we need to actually migrate.

373
00:20:57,580 --> 00:20:59,890
So first off, let's check here.

374
00:20:59,940 --> 00:21:03,580
Looks like we have some hints telling us we maybe have something up.

375
00:21:04,030 --> 00:21:07,990
So in the author last name, character fields must define a maxlength attribute.

376
00:21:08,350 --> 00:21:13,370
So let me come down here to author, and it looks like you just typed in 200 instead of the maxlength.

377
00:21:13,390 --> 00:21:14,170
I'll fix that now.

378
00:21:14,920 --> 00:21:19,150
So Maxlength is equal to 200 and we'll set that up first name there.

379
00:21:20,050 --> 00:21:26,380
And so then on book author it says The field specifies Anderlecht is equal to set nul but cannot be

380
00:21:26,380 --> 00:21:30,390
null set null equals true argument on the field or change the oblique rule.

381
00:21:30,400 --> 00:21:32,710
So we'll come back to author or excuse me, book author.

382
00:21:33,070 --> 00:21:34,510
Here it says set null.

383
00:21:34,900 --> 00:21:37,480
Which means I should say null can be true.

384
00:21:38,830 --> 00:21:39,320
There we go.

385
00:21:39,340 --> 00:21:40,750
So now we'll go ahead and save that.

386
00:21:41,350 --> 00:21:43,360
And it looks like there was no complaints.

387
00:21:43,360 --> 00:21:47,290
So did those checks for us, which is really cool that it's actually doing those checks even just on

388
00:21:47,290 --> 00:21:47,710
saving.

389
00:21:48,220 --> 00:21:52,390
So what I'm going to do now is I should be ready to actually run the migrations.

390
00:21:52,390 --> 00:22:01,960
So let me do controls, see here and say up here I'm going to say Python managed that py make migrations.

391
00:22:04,280 --> 00:22:04,700
Enter.

392
00:22:04,970 --> 00:22:05,960
Looks like it's all working though.

393
00:22:05,960 --> 00:22:07,820
I'll say python manage that.

394
00:22:07,820 --> 00:22:08,600
Pi migrate.

395
00:22:08,780 --> 00:22:12,230
Hopefully we didn't permit any typos there, but it looks like everything worked.

396
00:22:12,440 --> 00:22:14,060
Okay, so we talked a lot of code.

397
00:22:14,390 --> 00:22:16,010
If you're starting to get errors here, definitely.

398
00:22:16,010 --> 00:22:19,610
It's kind of thing where you probably want to just reference our provided code.

399
00:22:20,060 --> 00:22:20,390
Okay.

400
00:22:20,900 --> 00:22:21,320
Thanks.

401
00:22:21,320 --> 00:22:22,760
And I will see you at the next lecture.

