1
00:00:05,230 --> 00:00:10,440
Welcome back in this lecture we're going to go through an example solution for that database project

2
00:00:10,530 --> 00:00:13,290
adding in owners to our actual Web site.

3
00:00:13,300 --> 00:00:14,530
Let's get started.

4
00:00:14,530 --> 00:00:14,830
OK.

5
00:00:14,860 --> 00:00:18,850
I'm going to start with the actual adoption site thought Pif file.

6
00:00:18,850 --> 00:00:25,560
I will scroll down to where I have my models and I'm going to add in a model for an owner class.

7
00:00:25,570 --> 00:00:32,230
Well I'm going to zoom in just a little bit here so you can see this better will say class owner.

8
00:00:32,480 --> 00:00:39,230
And then we're going to inherit from D-B model and we can give her own table name if we want say a table

9
00:00:39,230 --> 00:00:42,410
name is equal to owners.

10
00:00:42,650 --> 00:00:44,500
Then each owner is going to have a unique ID.

11
00:00:46,980 --> 00:00:54,350
If we call them and they will say the integer type and this will be a primary key.

12
00:00:55,630 --> 00:01:04,580
So the primary key equal to true then I will say name is equal to D-B column and we're going to say

13
00:01:04,580 --> 00:01:07,210
divi text.

14
00:01:07,390 --> 00:01:12,880
And then finally we need to actually set up the relationship between owner and puppy.

15
00:01:12,970 --> 00:01:15,460
So we do this by editing both classes.

16
00:01:15,460 --> 00:01:16,800
I'll start off with puppy.

17
00:01:16,860 --> 00:01:19,670
Remember we're treating this as a one to one relationship.

18
00:01:19,900 --> 00:01:24,720
So I will say the puppies have an owner call them and it's a relationship.

19
00:01:25,000 --> 00:01:34,520
So say a relationship owner and the back reference for the other owner class is just going to be puppy

20
00:01:37,040 --> 00:01:42,380
and since it's a one to one relationship we say use list is equal to false.

21
00:01:42,570 --> 00:01:49,170
And now that we've set up the relationship in the puppy class I can now say we have a puppy ID column

22
00:01:49,170 --> 00:01:58,090
for the owner and we'll stick that equal to a column and inside of this column it's going to be an integer.

23
00:01:58,160 --> 00:02:00,800
Now we're going to connect it to the foreign key

24
00:02:04,450 --> 00:02:09,840
D-B the foreign key and we're going to pasan puppies the ID.

25
00:02:09,880 --> 00:02:14,200
Again what we're doing here is connecting the owner put the ID column to have the relationship.

26
00:02:14,350 --> 00:02:16,270
Let's make sure we spell integer right here.

27
00:02:16,420 --> 00:02:23,410
The real integer to have the correct relationship between the puppy owner column and the owner puppie

28
00:02:23,470 --> 00:02:24,730
ID column.

29
00:02:24,730 --> 00:02:30,260
Once we've done that we can go ahead and add in that method for owner.

30
00:02:30,460 --> 00:02:36,530
Say if it they'll take self and then create an owner.

31
00:02:36,860 --> 00:02:40,400
You give the owner a name and then you also assign a puppy to them.

32
00:02:41,530 --> 00:02:48,540
And then hear will say self-taught name is equal to name and self-taught puppy ID is equal to puppy

33
00:02:48,540 --> 00:02:50,140
ID.

34
00:02:50,380 --> 00:03:04,900
And then let's add a representation of our actual model here and we'll say return owner name say self-taught

35
00:03:04,900 --> 00:03:05,200
name

36
00:03:08,450 --> 00:03:14,120
and now that we've had a good representation of our owner let's adjust a representation of a puppy to

37
00:03:14,120 --> 00:03:18,440
adjust for whether or not they're assigned an owner yet and we're going to do this just like we did

38
00:03:18,530 --> 00:03:30,300
in a previous lecture will say if softball owner return let's say puppy name is self-taught.

39
00:03:30,300 --> 00:03:32,980
Name and they will say.

40
00:03:33,000 --> 00:03:39,270
And the owner is also a self-taught owner name

41
00:03:43,090 --> 00:03:44,990
and then will say else.

42
00:03:45,360 --> 00:03:55,920
And in that case we'll return the puppy name but will say and no owner assigned yet.

43
00:03:56,030 --> 00:03:58,810
All right so those are the main changes we need to do to our adoption site.

44
00:03:58,820 --> 00:04:04,580
Again we're adding a new column to puppy it's an owner and it's going to be a relationship with owner.

45
00:04:04,610 --> 00:04:09,470
The back reference for the owner is this puppy class and then we say use list is equal to false because

46
00:04:09,530 --> 00:04:12,160
it's a one to one relationship.

47
00:04:12,160 --> 00:04:16,350
Now we want to make sure all these changes actually occur in our database.

48
00:04:16,630 --> 00:04:19,900
So I'll start off by saying flask D-B.

49
00:04:20,170 --> 00:04:25,200
And if you're starting for brand new set of files where you don't have the migrations yet you're going

50
00:04:25,200 --> 00:04:27,240
to need to say flask could be in it.

51
00:04:27,430 --> 00:04:28,510
But I've already done that.

52
00:04:28,510 --> 00:04:32,840
You only need to run that once if you already have your migrations folder ready to go.

53
00:04:32,890 --> 00:04:40,720
In our case we'll say flask D.B and I'm going to migrate and I'll get my message to say added owners

54
00:04:41,590 --> 00:04:46,050
run that and then you should see some commands after just a little bit of time.

55
00:04:46,090 --> 00:04:48,000
So it looks like we have those migration files.

56
00:04:48,010 --> 00:04:50,550
Now let's actually apply the changes by saying flask.

57
00:04:50,890 --> 00:04:52,930
DBI upgrade.

58
00:04:52,930 --> 00:04:58,260
Hit enter and now we should have owners successfully in our database.

59
00:04:58,300 --> 00:05:03,720
Once you've done that let's create a view and a form that lets you add new owners to the database.

60
00:05:03,790 --> 00:05:09,290
And when you add these owners we can also connect them to be a puppy with a puppy ID.

61
00:05:09,430 --> 00:05:14,470
So it's kind of up to you whether you want to first focus on the same HMO file or focus first on the

62
00:05:14,470 --> 00:05:16,040
actual view function.

63
00:05:16,060 --> 00:05:19,840
I tend to focus more on my Python and then connect it to the HTL.

64
00:05:19,900 --> 00:05:24,140
So let's go if that route will create a new view.

65
00:05:24,160 --> 00:05:29,380
So we have index at pup list up and then delete the pup.

66
00:05:29,410 --> 00:05:31,500
How about right below the index.

67
00:05:31,510 --> 00:05:39,810
We create a new view for adding an owner will say appro and I will say this goes to add underscore owner

68
00:05:41,460 --> 00:05:48,970
and since there will be a form on this page to add an owner will say methods is equal to gets and post

69
00:05:50,470 --> 00:05:59,970
once than that we're going say the chef and owner and this function is going to be really similar to

70
00:06:00,000 --> 00:06:04,560
add a pup except something we need to do save form is equal to.

71
00:06:04,560 --> 00:06:11,180
And so far I have a forum for adding puppies and the form for the little puppies but I don't have a

72
00:06:11,220 --> 00:06:12,840
forum for adding owners.

73
00:06:12,840 --> 00:06:17,230
So let's quickly go to our forms of Pi file and adjust for that.

74
00:06:17,280 --> 00:06:19,380
So we'll come over to forms that pi.

75
00:06:19,430 --> 00:06:26,070
I have an ad form a delete form what I'm going to do now is create an add owner form we'll just say

76
00:06:26,070 --> 00:06:38,650
class add owner form we inherit from flask form and an owner is going to need a name the puppy ID and

77
00:06:38,650 --> 00:06:40,490
then just a submit button.

78
00:06:40,490 --> 00:06:52,110
So say name is equal to a String Field name of owner then we'll say pup underscore ID is equal to an

79
00:06:52,150 --> 00:06:59,640
integer fields will say integer field and say Id of puppie.

80
00:06:59,870 --> 00:07:01,580
And then finally we'll just have that user submit.

81
00:07:01,580 --> 00:07:11,830
So say submit is equal to a submit field and we'll say add owner.

82
00:07:11,920 --> 00:07:17,790
OK so now that we create or add or form what I'm going to do is back over an adoption site I'm going

83
00:07:17,790 --> 00:07:18,730
to study here.

84
00:07:18,840 --> 00:07:22,020
But before I do that actually to import it from forms up PI.

85
00:07:22,470 --> 00:07:26,550
So up here where we said from forms we've imported add form and delete form.

86
00:07:26,670 --> 00:07:34,650
So now we're going to tack on an owner form and then it can come down here and inside my own review

87
00:07:35,070 --> 00:07:39,270
I'll say form is equal to add owner form.

88
00:07:39,330 --> 00:07:41,570
So create an instance of an owner form.

89
00:07:41,580 --> 00:07:46,160
Now I could just go through checking it and then adding the owner to our database.

90
00:07:46,560 --> 00:07:54,680
We'll see if the form is valid when you click submit and when to grab the name of that owner by saying

91
00:07:54,680 --> 00:08:04,340
form name data can zoom in just a little bit here and then I'm going to grab the pop ID by saying Form

92
00:08:04,790 --> 00:08:11,420
pop underscore ID and grab the data from that one and then add that new owner to the database.

93
00:08:11,420 --> 00:08:20,510
So we'll say this new owner is equal to an owner model where we pass in the name and the pump ID number

94
00:08:21,440 --> 00:08:24,420
and then we'll say D-B session.

95
00:08:24,700 --> 00:08:32,430
I'm going to add a new owner and then we'll commit those changes will say D-B session come in.

96
00:08:32,660 --> 00:08:36,580
Once you've added a new owner we should redirect them to the list page.

97
00:08:36,660 --> 00:08:48,310
So I will say return a redirect that your L4 and will say list pup and then the outside return.

98
00:08:48,360 --> 00:08:51,800
That's just going to be the template we end up adding for owners.

99
00:08:51,810 --> 00:09:00,290
So say add owner T.M. and we'll pass in this form equals to form and you probably noticed that we yet

100
00:09:00,330 --> 00:09:01,740
don't have an ad over that age.

101
00:09:01,750 --> 00:09:09,280
Tim L we have an ad pup or just an ad HMO but we haven't had a separate template for just adding owners.

102
00:09:09,330 --> 00:09:10,550
So let's create one.

103
00:09:10,860 --> 00:09:17,640
We'll come over to templates create a new file we'll call add underscore owner the HTL to make sure

104
00:09:17,640 --> 00:09:20,450
it matches what we wrote here and rendered template.

105
00:09:20,680 --> 00:09:23,730
I'm going to save that now under an owner.

106
00:09:23,730 --> 00:09:29,490
Well we need to do is set up the form and then we also want to extend from our base class that way we

107
00:09:29,490 --> 00:09:31,390
all have the same stylings.

108
00:09:31,470 --> 00:09:32,510
So we're going to do here.

109
00:09:32,550 --> 00:09:44,540
We'll start off with extends from based on each T.M. we'll set up a block ice content and then I'm going

110
00:09:44,540 --> 00:09:46,610
to create a jumbotron.

111
00:09:46,610 --> 00:09:51,000
Some of this is optional though we'll see an owner

112
00:09:54,700 --> 00:10:06,820
and after that we'll just have some explanation that a new owner with the form below and a new owner.

113
00:10:06,830 --> 00:10:07,640
There you go.

114
00:10:07,670 --> 00:10:09,560
Now it's time to insert the form.

115
00:10:09,590 --> 00:10:15,770
So still inside the show Latron I'm going to say form have an H him a form.

116
00:10:15,780 --> 00:10:16,780
I don't need action.

117
00:10:16,800 --> 00:10:23,790
Don't need class I just need to say methodis post and just on some Wes's you have to sometimes capitalize

118
00:10:23,790 --> 00:10:24,230
it.

119
00:10:24,270 --> 00:10:26,920
It should technically not be an issue but just keep that in mind.

120
00:10:27,240 --> 00:10:30,090
We'll say form and then we'll say critter hit instead.

121
00:10:32,540 --> 00:10:35,690
And we'll say a label for the name and the label for the puppy.

122
00:10:36,260 --> 00:10:39,070
So form name label.

123
00:10:41,610 --> 00:10:53,900
And then form that name and then form and it's pup underscore ID attribute the label for that and then

124
00:10:53,900 --> 00:10:58,360
set form the pup underscore ID.

125
00:10:59,640 --> 00:11:02,200
Let's make sure of open close princes or on name as well.

126
00:11:03,920 --> 00:11:06,620
Whoops there we go.

127
00:11:06,620 --> 00:11:08,840
And then we'll have the submit button.

128
00:11:08,840 --> 00:11:12,270
We'll see form thought submit.

129
00:11:12,710 --> 00:11:18,590
Let's see if everything worked Baxley running our site will say Python adoption site and it looks like

130
00:11:18,590 --> 00:11:23,440
it's running springing over our browser and check to make sure it's working.

131
00:11:23,450 --> 00:11:29,510
So I come over here and unfortunately we see just home add a pup list the pups and the pup something

132
00:11:29,510 --> 00:11:35,580
that forgot to do is actually set a new link in our NAV bar to go to the ad owner page.

133
00:11:35,600 --> 00:11:41,510
So let's go back and we need to adjust our based table in order to make that happen.

134
00:11:41,570 --> 00:11:48,880
So when I come over here to base let each team know and it's just going to copy this and then add one

135
00:11:48,880 --> 00:11:50,050
more.

136
00:11:50,220 --> 00:11:56,140
Everything is going to be the same except instead of DTL pup we see what view we have for an owner.

137
00:11:56,200 --> 00:12:04,630
So that was just the owner going to copy it and it's back to basics T.M. So people say an owner and

138
00:12:04,630 --> 00:12:08,460
then that or this will say an owner.

139
00:12:08,510 --> 00:12:09,710
All right let's save that.

140
00:12:09,740 --> 00:12:11,510
Now that should be linked correctly.

141
00:12:11,750 --> 00:12:17,390
Do Control-C and let's bring back our Web site here.

142
00:12:17,430 --> 00:12:18,940
And now I see an owner.

143
00:12:19,170 --> 00:12:23,870
So I see my list the pups Frankie and the owner signed to Sammy and the owner signed yet.

144
00:12:24,120 --> 00:12:30,180
So remember I'm using the exact same database that I had before from the lectures where we showed you

145
00:12:30,180 --> 00:12:32,330
how to build the basic adoption site.

146
00:12:32,460 --> 00:12:37,590
So Frankie has an idea of two and Sammy has an idea or three because we deleted rufous back in those

147
00:12:37,590 --> 00:12:38,500
videos.

148
00:12:38,530 --> 00:12:44,100
So keep in mind you can always delete and start everything over again in case you're having issues with

149
00:12:44,100 --> 00:12:46,440
matching IDs to owners.

150
00:12:47,270 --> 00:12:52,940
So we'll come back to owner or the name of the owner will say is Eddie and then the idea of the puppy

151
00:12:53,420 --> 00:12:58,700
if I try putting in one that's going to retrieve an error because we deleted rufous So it's put into

152
00:12:59,100 --> 00:13:04,010
I'll say an owner and we say here's a list of all puppies all available puppies.

153
00:13:04,010 --> 00:13:04,840
Looks like freaky.

154
00:13:04,850 --> 00:13:08,210
An owner is Eddie and Sammy and now owners are assigned.

155
00:13:08,210 --> 00:13:12,940
So now let's go ahead and add an owner to Sammy so I'll come in here.

156
00:13:12,950 --> 00:13:15,360
Say an owner the name of the owner.

157
00:13:15,560 --> 00:13:19,470
We'll just say Edward idea of puppy.

158
00:13:19,520 --> 00:13:21,260
So Frankie should be too.

159
00:13:21,260 --> 00:13:24,300
So we'll say idea of puppy or excuse me should be three.

160
00:13:24,380 --> 00:13:25,610
So say three.

161
00:13:25,730 --> 00:13:28,380
Say an owner an icy puppy named Sammy.

162
00:13:28,550 --> 00:13:35,060
And owner is Edward So it looks like we're able to successfully connect owners to their puppies.

163
00:13:35,060 --> 00:13:43,340
Keep in mind right now if I try to put in some owner so maybe I do add a bunch of these and I try to

164
00:13:43,340 --> 00:13:48,570
put in the idea of a puppy that doesn't exist in the database such as one click at owner.

165
00:13:48,570 --> 00:13:49,940
Nothing's going to happen here.

166
00:13:50,030 --> 00:13:51,920
You're just going to see Eddie and Edward.

167
00:13:51,920 --> 00:13:56,150
You're not actually going to get a connection because even though the owners in that database there

168
00:13:56,150 --> 00:13:59,010
is no puppy that links up with puppy ID one.

169
00:13:59,030 --> 00:14:06,560
So just like I said at owner we do a bunch of these here and then the puppy 10000 click at owner.

170
00:14:06,650 --> 00:14:11,540
The database has no problem with that except there's no puppies for those particular IDs.

171
00:14:11,660 --> 00:14:14,200
So you don't see it on the list of all available puppies.

172
00:14:14,530 --> 00:14:15,070
OK.

173
00:14:15,320 --> 00:14:17,530
That was the very basics of the solutions.

174
00:14:17,540 --> 00:14:20,360
Feel free to share your own solutions in the Q&amp;A forums.

175
00:14:20,360 --> 00:14:21,810
We'll see you at the next lecture.
