1
00:00:05,140 --> 00:00:05,500
All right.

2
00:00:05,500 --> 00:00:06,310
Welcome back.

3
00:00:06,310 --> 00:00:11,230
We're going to continue off right where we left off last time and first thing we need to do is make

4
00:00:11,230 --> 00:00:14,100
sure that we set our flask application variable correctly.

5
00:00:14,230 --> 00:00:22,600
Since I renamed my file to be models not pie I'm going to set a flask app equal to models of Pi and

6
00:00:22,600 --> 00:00:24,220
this is something you're always going to have to do.

7
00:00:24,220 --> 00:00:28,150
In case you play around the name of your actual application file.

8
00:00:28,150 --> 00:00:29,310
So I will set that.

9
00:00:29,320 --> 00:00:34,480
Remember if you're on a Mac OS or Linux you use export instead of set and you shouldn't see anything

10
00:00:34,480 --> 00:00:36,330
output from that.

11
00:00:36,330 --> 00:00:39,030
So here we see flasks at next.

12
00:00:39,050 --> 00:00:42,250
I'm going to say flask D-B in it.

13
00:00:42,290 --> 00:00:45,260
So I'm going to initiate my actual database.

14
00:00:45,260 --> 00:00:49,940
And if you've done that they should be able to expand this and see the migrations folder.

15
00:00:49,940 --> 00:00:53,920
So those migrations fall there and then we have these options right here.

16
00:00:53,960 --> 00:01:01,340
So the next thing I need to do is say flask and then I'm going to actually set my migrations and the

17
00:01:01,340 --> 00:01:08,430
way we do that is by typing Flast DBI migrate and then optimally you can add a message flag.

18
00:01:08,440 --> 00:01:11,870
So dash em and I would recommend that you always do this.

19
00:01:11,900 --> 00:01:17,570
So say initial migration enter.

20
00:01:17,860 --> 00:01:22,680
That's going to set the migration file and then to actually make sure everything gets run you say flask.

21
00:01:22,690 --> 00:01:27,960
The upgrade enter and now we have everything connected.

22
00:01:28,000 --> 00:01:29,430
So now we're ready to go.

23
00:01:29,440 --> 00:01:34,300
What I'm going to do is in the same folder that models that pi is in.

24
00:01:34,330 --> 00:01:37,480
So right now I have it all located under flask examples.

25
00:01:37,480 --> 00:01:44,220
I'm going to create a new file and I'm just going to call it basic dot pi and you really can call us

26
00:01:44,230 --> 00:01:45,340
whatever you want.

27
00:01:45,340 --> 00:01:56,260
So I have here now my file basic thought PI and this is a file where I'm going to create entries into

28
00:01:56,260 --> 00:02:00,550
the tables and then I'm actually going to print out the results.

29
00:02:00,550 --> 00:02:03,470
So again you can call this file pretty much whatever you want.

30
00:02:03,730 --> 00:02:09,010
So let's start off by saying from models.

31
00:02:09,210 --> 00:02:12,670
Imports then wanted to import D-B.

32
00:02:12,750 --> 00:02:17,370
I'm going to import my puppy model the owner model and the toy model.

33
00:02:17,740 --> 00:02:23,700
So I always start off by creating loops creating two puppies.

34
00:02:23,860 --> 00:02:31,120
So it's great to put the objects will say rufous is a puppy objects where we say puppy area or we need

35
00:02:31,120 --> 00:02:32,300
to pass and the puppy name.

36
00:02:32,360 --> 00:02:38,170
So say Rufus if you take a look back at the models were essentially just related to the IT method right

37
00:02:38,170 --> 00:02:38,710
now.

38
00:02:38,740 --> 00:02:44,140
So puppy just needs a name to start off with and then later on we can add owners to the puppies.

39
00:02:44,140 --> 00:02:48,270
So say Rufus is a puppy and let's make Fido a puppy as well.

40
00:02:49,360 --> 00:02:50,340
We say puppy.

41
00:02:50,500 --> 00:02:54,370
And then we pass on the name Fido next.

42
00:02:54,400 --> 00:02:57,230
I'm going to add puppies to the database.

43
00:02:57,260 --> 00:02:59,760
So the way we do that we'll say ad puppies.

44
00:03:01,150 --> 00:03:02,960
To D-B.

45
00:03:03,050 --> 00:03:08,280
Remember we can grab that database and then create a session.

46
00:03:08,350 --> 00:03:13,840
And if I have a list of objects I want to pass I can say at all in this case and want to add rufous

47
00:03:13,930 --> 00:03:21,650
and phyto they'll say D-B session and I'm going to commit those changes.

48
00:03:21,650 --> 00:03:25,010
So once those changes have been saved I'm going to check on this.

49
00:03:25,020 --> 00:03:33,490
So we'll check it by saying Prince will say puppy queery all.

50
00:03:33,720 --> 00:03:37,680
And that's going to allow us to return all the puppies in the database.

51
00:03:37,790 --> 00:03:43,910
So remember as I mentioned previously we're actually creating scripts that add new entries into our

52
00:03:43,910 --> 00:03:44,780
database.

53
00:03:44,780 --> 00:03:50,480
If you run the script multiple times you may get errors because you may be adding duplicates or trying

54
00:03:50,480 --> 00:03:52,270
to delete something that doesn't exist.

55
00:03:52,280 --> 00:03:53,110
So keep that in mind.

56
00:03:53,120 --> 00:03:58,970
I write this script in a way that's meant to be only run once I'm going to collapse my left hand tab

57
00:03:58,970 --> 00:04:01,100
here and continue on.

58
00:04:01,100 --> 00:04:08,150
So here I should see rufous and phyto and remember the puppies the way they're printed out and represented

59
00:04:08,600 --> 00:04:13,940
it should be something like Hey puppy name is Rufus and has no idea puppy name is Fido and has no owner

60
00:04:13,940 --> 00:04:14,270
yet.

61
00:04:14,480 --> 00:04:17,930
So that's what's going to be printed out when you check on all those puppies.

62
00:04:18,500 --> 00:04:22,580
So now I'm willing to just grab Rufus from the database.

63
00:04:23,000 --> 00:04:32,000
So I'll say Rufus is now equal to puppy queery so we can actually Graham are basically treating this

64
00:04:32,060 --> 00:04:40,190
as an Aurum and then I use the filter underscore by Methot off of this or the name is equal to.

65
00:04:40,550 --> 00:04:42,770
And I will say Rufus here.

66
00:04:43,610 --> 00:04:50,820
And what happens is we actually get back a list of items so one way to do this is to say first.

67
00:04:50,900 --> 00:04:54,660
So give me back the first puppy whose name is Rufus.

68
00:04:54,680 --> 00:04:57,620
And in this case it should just be this Rufus.

69
00:04:57,620 --> 00:05:03,470
If you have multiple puppies whose name you think is Rufus The other alternative is to say all.

70
00:05:03,560 --> 00:05:08,060
And I'll return a list of all the puppies whose name is Rufus and if he's just one of the first item

71
00:05:08,060 --> 00:05:11,660
off of that then you could do something like indexing to zero.

72
00:05:12,050 --> 00:05:18,880
So right now I'm just going to say give me that first puppy that I'm going to create an owner for Rufus.

73
00:05:19,090 --> 00:05:21,460
So let's create an owner object.

74
00:05:23,500 --> 00:05:31,690
I will say Jose variable is an owner and the owner has the name Jose.

75
00:05:32,040 --> 00:05:38,010
And then remember I just grabbed Rufus and Rufus is a puppy inside the database.

76
00:05:38,010 --> 00:05:41,040
It was the first puppy whose name was Rufus inside that database.

77
00:05:41,040 --> 00:05:46,700
And if we take a look back at Rufus who is a puppy Rufus object that has an ID attribute.

78
00:05:46,890 --> 00:05:53,580
And if I take a look at the owners when I create an owner and is supply the name of the owner and then

79
00:05:53,640 --> 00:05:55,130
also the puppy ID.

80
00:05:55,410 --> 00:06:00,060
So then that means I need to grab Rufus's ID or some other puppies ID.

81
00:06:00,090 --> 00:06:06,930
So since I already have Rufus as an object I'm going say that this owner's Jose and the puppy ID is

82
00:06:06,930 --> 00:06:09,230
going to be rufous ID.

83
00:06:09,390 --> 00:06:12,690
So that's the id attribute off Rufus right here.

84
00:06:12,840 --> 00:06:14,780
Who's the first puppy objects returned.

85
00:06:14,790 --> 00:06:19,960
When I filtered by name is equal to Rufus which is this puppy that I created a peer.

86
00:06:19,980 --> 00:06:28,620
So I just created owner Jose with a rufous Id not going to give some toys to Rufus So let's give Rufus

87
00:06:28,620 --> 00:06:32,740
some toys.

88
00:06:32,740 --> 00:06:38,380
Now this is a one to many relationships we can give multiple toys to Rufus so create two toys will say

89
00:06:38,380 --> 00:06:43,050
toy one and let's take a look at the method for toy.

90
00:06:43,050 --> 00:06:49,510
If we go back to models of Pi scroll up toys they have an item name and a puppy ID for who they belong

91
00:06:49,510 --> 00:06:50,350
to.

92
00:06:50,350 --> 00:06:51,380
So the first toy.

93
00:06:51,550 --> 00:06:53,640
Let's just say it's a chew toy.

94
00:06:54,900 --> 00:06:56,500
And we'll also have it belong to Rufus.

95
00:06:56,500 --> 00:07:04,440
Say Rufus thought Id And I'll create another toy object toy too and we'll say this is a toy I'll say

96
00:07:04,440 --> 00:07:11,020
it's a ball and it's also going to belong to Rufus connected by Rufus's ID.

97
00:07:11,040 --> 00:07:11,450
OK.

98
00:07:11,610 --> 00:07:14,740
So once we have all that when you commit those changes.

99
00:07:14,740 --> 00:07:19,350
So you actually commit the owners and these two toys to our database and the way we're going to do that

100
00:07:19,500 --> 00:07:26,280
is just by simply saying D-B session and then I'm going to add all and we pass and that list will say

101
00:07:26,340 --> 00:07:29,670
add Jose add toy one and add toy to.

102
00:07:29,820 --> 00:07:32,940
Notice how flexible this add all method is.

103
00:07:32,950 --> 00:07:38,400
I'm passing in different object types but the session is not going to complain because it understands

104
00:07:38,490 --> 00:07:44,460
that these particular objects relate to particular tables because they inherit from db that model.

105
00:07:44,490 --> 00:07:49,040
So a lot of that logic is taken care of for you already on the backend which is why we're able just

106
00:07:49,050 --> 00:07:52,770
to say D-B session and all of these items to my tables.

107
00:07:53,640 --> 00:07:53,910
All right.

108
00:07:53,910 --> 00:07:55,900
Once we've done that we want to commit those changes.

109
00:07:56,800 --> 00:08:03,490
Well say D-B session commit and now we're going to grab rufous again.

110
00:08:03,510 --> 00:08:10,060
So let's grab Rufus after those admissions.

111
00:08:10,780 --> 00:08:22,350
So I'll say rufous again and say Rufus is equal to puppy do a query and say filter by where the name

112
00:08:22,530 --> 00:08:24,990
is equal to Rufus.

113
00:08:25,080 --> 00:08:31,490
Let's make sure that's a stream Rufus and then grabbed the first puppy that happens to be true.

114
00:08:32,320 --> 00:08:35,270
And then print outs Rufus

115
00:08:37,990 --> 00:08:44,280
so we should expect is when you print out rufous here Rufus now has an owner attached to it Jose.

116
00:08:44,470 --> 00:08:51,900
So the first time that I print rufous in fact I'm going to rafter call him or print rufous again so

117
00:08:52,260 --> 00:08:56,370
I print both puppies I should see Rufus and phyto with no owners.

118
00:08:56,370 --> 00:09:00,660
Then I grab Rufus already from the database and I print him again by himself.

119
00:09:00,660 --> 00:09:02,650
So I should see Rufus hasna owner.

120
00:09:02,670 --> 00:09:05,300
I create the owner object create the two toys.

121
00:09:05,460 --> 00:09:07,450
Commit them and add them to the database.

122
00:09:07,590 --> 00:09:14,070
And then when I read grab Rufus from the database and I print them out then I should see from up here

123
00:09:14,460 --> 00:09:19,680
that self-taught is true meaning I should see puppy name as Rufus and owner is Jose.

124
00:09:19,740 --> 00:09:23,520
So that's what I would expect on this print statement on 9:31.

125
00:09:23,970 --> 00:09:29,190
And recall that these puppies if you go back to models they also have a report toys method.

126
00:09:29,400 --> 00:09:30,440
So I'm going to copy that.

127
00:09:30,510 --> 00:09:36,030
And then I should see here are my toys and I should see a shoe tall Go chew toy and a ball.

128
00:09:36,030 --> 00:09:36,620
There you go.

129
00:09:38,030 --> 00:09:45,850
So we're going to say Prince Rufus and we'll say report toys.

130
00:09:47,110 --> 00:09:51,820
So let's take a look at that report toys and I think right now is just printing it.

131
00:09:51,820 --> 00:09:57,460
Let's see if that works we may need to actually just make that into a return but that will probably

132
00:09:57,460 --> 00:09:57,830
work great.

133
00:09:57,850 --> 00:10:03,530
There let's say this and see if we have any issues running it say Python basic thought pi.

134
00:10:03,580 --> 00:10:07,660
So I should see a bunch of print statements enter here.

135
00:10:07,660 --> 00:10:09,270
There we go.

136
00:10:09,300 --> 00:10:14,950
So it says here puppy name is rufous and has no owner yet puppy name is Fido and has no owner yet.

137
00:10:15,300 --> 00:10:17,750
Then puppy name is rufous has no owner yet.

138
00:10:17,790 --> 00:10:23,850
Then puppy names Rufus owners Jose here at my toys chew toy and ball and at the end they got none because

139
00:10:23,850 --> 00:10:25,580
I'm kind of just printing out the results of this.

140
00:10:25,590 --> 00:10:30,400
I think this should've just been rufous that report toys because inside a model's section are returning

141
00:10:30,400 --> 00:10:32,150
anything it's already pretty enough for you.

142
00:10:32,190 --> 00:10:36,250
So we didn't need to actually say none there or we didn't need a print.

143
00:10:36,270 --> 00:10:36,840
All right.

144
00:10:36,840 --> 00:10:39,960
So that's the very basics of actually having the relationship.

145
00:10:39,960 --> 00:10:46,120
So the main things and notice here is we're able to now connect this puppy to a list of toys.

146
00:10:46,110 --> 00:10:50,110
So that's a one to many relationship and we're also able to connect an owner objects.

147
00:10:50,220 --> 00:10:54,120
And that's a one to one leader and we'll discuss more complex relationships.

148
00:10:54,150 --> 00:10:59,520
And if you're interested in them right now you can always check out the sequel alchemy code or documentation

149
00:10:59,640 --> 00:11:01,580
to get those sort of examples there.

150
00:11:01,770 --> 00:11:05,880
And as a quick reminder you can always delete things from the database by using D-B that session that

151
00:11:05,920 --> 00:11:08,580
delete and then deleting the puppy from it.

152
00:11:09,000 --> 00:11:12,030
OK so we've discussed already quite a lot of detail.

153
00:11:12,210 --> 00:11:18,090
How do create models now what we need to do is start talking about how we actually connect these models

154
00:11:18,420 --> 00:11:24,290
to the views because we haven't actually seen this in regards to a Web site.

155
00:11:24,300 --> 00:11:29,550
So now that we understand everything is going off the models the databases school light and migrations.

156
00:11:29,670 --> 00:11:34,870
Let's take all of that knowledge and connect it to what we already understand about serving templates.

157
00:11:34,980 --> 00:11:40,710
That way we can have pretty much our full functioning Web site where we're able to accept the form of

158
00:11:40,740 --> 00:11:47,680
input and then use these sort of at all and commit commands inside of our flask application to add a

159
00:11:47,700 --> 00:11:54,180
user form data into a database and then we can also read it and return it back on another page.

160
00:11:54,180 --> 00:11:58,810
So we're finally in the very next lecture actually going to start working on the most functional web

161
00:11:58,810 --> 00:12:00,160
site we've done so far.

162
00:12:00,210 --> 00:12:01,790
I'm really excited for that next step.

163
00:12:01,800 --> 00:12:04,070
It's really a huge leap forward in your abilities.

164
00:12:04,170 --> 00:12:09,000
So we'll see you in the next lecture where we begin to discuss databases in views either.
