1
1

00:00:02,150  -->  00:00:03,770
<v Jose>Hey guys welcome back.</v>
2

2

00:00:03,770  -->  00:00:07,490
In this video we're going to be adding a few more methods
3

3

00:00:07,490  -->  00:00:11,610
to our model just to finalise it and make sure that the new
4

4

00:00:11,610  -->  00:00:14,940
model classes that we'll be creating all have
5

5

00:00:14,940  -->  00:00:19,000
these methods, and there are gonna be a few of them.
6

6

00:00:19,000  -->  00:00:23,450
For example we're going to have something like save to Mongo
7

7

00:00:23,450  -->  00:00:25,200
which is going to be in every model.
8

8

00:00:25,200  -->  00:00:28,520
We're going to move that to our model class,
9

9

00:00:28,520  -->  00:00:31,800
we're gonna have things like get by ID
10

10

00:00:31,800  -->  00:00:34,600
which is probably gonna be necessary in every model
11

11

00:00:34,600  -->  00:00:39,174
and even if not all models use this get by ID,
12

12

00:00:39,174  -->  00:00:42,630
most of them will so it's also a good thing
13

13

00:00:42,630  -->  00:00:45,143
to move into our model base class.
14

14

00:00:46,210  -->  00:00:48,160
So I'm gonna do just that, I'm going to implement
15

15

00:00:48,160  -->  00:00:50,750
a few methods into this model base class
16

16

00:00:50,750  -->  00:00:52,443
and then we're gonna talk about them.
17

17

00:01:07,860  -->  00:01:10,720
So I added these two methods now,
18

18

00:01:10,720  -->  00:01:14,520
and we're got save to Mongo and remove from Mongo.
19

19

00:01:14,520  -->  00:01:19,150
Save to Mongo is going to call database.update,
20

20

00:01:19,150  -->  00:01:21,070
this is something that doesn't exist yet in our
21

21

00:01:21,070  -->  00:01:26,070
database class and update is going to insert a new thing
22

22

00:01:26,240  -->  00:01:30,090
into the database, unless there is already
23

23

00:01:30,090  -->  00:01:33,543
something in the database matching this underscore ID.
24

24

00:01:34,490  -->  00:01:37,660
So we're gonna run this search and if something comes back
25

25

00:01:37,660  -->  00:01:40,750
we're gonna update that thing instead of inserting
26

26

00:01:40,750  -->  00:01:43,800
a new one, this is called upserting,
27

27

00:01:43,800  -->  00:01:46,670
and it's a very common thing to do when you're dealing
28

28

00:01:46,670  -->  00:01:49,010
with unique elements.
29

29

00:01:49,010  -->  00:01:51,090
Remove from Mongo is going to do just that.
30

30

00:01:51,090  -->  00:01:53,790
It's going to take something away from MongoDB,
31

31

00:01:53,790  -->  00:01:56,150
all of the things that match this query here,
32

32

00:01:56,150  -->  00:01:58,390
all of the things that have this underscore ID
33

33

00:01:58,390  -->  00:02:00,280
are going to be removed.
34

34

00:02:00,280  -->  00:02:01,800
So we're gonna implement that in a second,
35

35

00:02:01,800  -->  00:02:04,900
but something else that we've got is self.collection
36

36

00:02:04,900  -->  00:02:09,020
and I talked earlier on about cls.collection
37

37

00:02:09,020  -->  00:02:13,470
and how we'd moved collection away from the init method
38

38

00:02:13,470  -->  00:02:16,533
and into a class level property.
39

39

00:02:17,720  -->  00:02:19,990
Can you still access the class property
40

40

00:02:19,990  -->  00:02:23,560
using self.collection, yes so this is
41

41

00:02:23,560  -->  00:02:25,250
a perfectly fine thing to do
42

42

00:02:25,250  -->  00:02:28,290
and it still allows us to access the current object
43

43

00:02:28,290  -->  00:02:30,300
selection property even if it's stored
44

44

00:02:30,300  -->  00:02:33,500
at the class level, rather than hard coding
45

45

00:02:33,500  -->  00:02:35,820
a specific string in here.
46

46

00:02:35,820  -->  00:02:38,393
So this is the way to access that,
47

47

00:02:39,330  -->  00:02:44,330
now you'll notice that self. underscore ID is unresolved
48

48

00:02:44,393  -->  00:02:49,000
and it's not easy to get rid of this warning
49

49

00:02:49,000  -->  00:02:51,540
because we don't want to do self. underscore ID
50

50

00:02:51,540  -->  00:02:54,820
equals something, we don't wanna do that.
51

51

00:02:54,820  -->  00:02:59,820
Because then we are going to still be setting it here
52

52

00:03:00,680  -->  00:03:03,570
and that's gonna be a little confusing.
53

53

00:03:03,570  -->  00:03:05,940
So for now we're gonna leave it as is.
54

54

00:03:05,940  -->  00:03:07,500
We're just going to accept that this
55

55

00:03:07,500  -->  00:03:10,950
is an unresolved attribute in the model class
56

56

00:03:10,950  -->  00:03:13,390
but it's going to be fine in the sub-class.
57

57

00:03:13,390  -->  00:03:16,844
Because they do have a self. underscore ID.
58

58

00:03:16,844  -->  00:03:21,844
Let's go over to our database and add those two methods.
59

59

00:03:23,040  -->  00:03:26,973
These are gonna be update and remove.
60

60

00:03:37,210  -->  00:03:38,950
So here's now we're gonna do it,
61

61

00:03:38,950  -->  00:03:41,680
we're going to receive the collection that
62

62

00:03:41,680  -->  00:03:45,160
we wanna modify, the things that we wanna look for
63

63

00:03:45,160  -->  00:03:47,110
and potentially update,
64

64

00:03:47,110  -->  00:03:50,630
and what we want to update them with.
65

65

00:03:50,630  -->  00:03:52,917
So everything that matches this query,
66

66

00:03:52,917  -->  00:03:56,090
all of the contents of that thing
67

67

00:03:56,090  -->  00:03:58,733
will be replaced by this data,
68

68

00:04:00,070  -->  00:04:03,002
and then we're gonna do update the query with data
69

69

00:04:03,002  -->  00:04:07,220
and we're gonna pass in this upsert equals true argument
70

70

00:04:07,220  -->  00:04:10,250
and what this is gonna do is add a new element
71

71

00:04:11,090  -->  00:04:14,650
with this data if the query returned nothing,
72

72

00:04:14,650  -->  00:04:18,140
and remember this is all PyMongo this module we've installed
73

73

00:04:18,140  -->  00:04:21,320
for interacting with MongoDB and what we're doing is
74

74

00:04:21,320  -->  00:04:24,463
we're just defining a slightly easier way of accessing it.
75

75

00:04:33,910  -->  00:04:36,300
And what we've done here is we have created
76

76

00:04:36,300  -->  00:04:39,950
the remove method and this is going to call
77

77

00:04:39,950  -->  00:04:44,717
the remove method in PyMongo and it's going to return
78

78

00:04:44,717  -->  00:04:49,160
all of the things that were removed matching this query.
79

79

00:04:49,160  -->  00:04:51,420
We're probably not gonna use anything that's being returned
80

80

00:04:51,420  -->  00:04:53,180
from this but nonetheless,
81

81

00:04:53,180  -->  00:04:54,930
it can be interesting to have that.
82

82

00:04:56,000  -->  00:04:57,660
So now we have our two methods here
83

83

00:04:57,660  -->  00:05:01,790
to update or insert and to remove we can go back
84

84

00:05:01,790  -->  00:05:03,983
to our model and continue adding.
85

85

00:05:15,000  -->  00:05:19,760
So what I've done now is I've added a find one by method,
86

86

00:05:19,760  -->  00:05:22,530
what it's gonna do is it's going to allow us to do something
87

87

00:05:22,530  -->  00:05:26,720
like item.find one by URL HTTPS
88

88

00:05:27,680  -->  00:05:29,877
you know whatever your URL is.
89

89

00:05:30,920  -->  00:05:34,900
This is then going to go into the database,
90

90

00:05:34,900  -->  00:05:38,260
it's gonna find one with the collection that's
91

91

00:05:38,260  -->  00:05:41,030
relevant to that class, in this case it's gonna be items
92

92

00:05:41,030  -->  00:05:46,030
and then it's gonna find it with URL is HTTPS bla.com
93

93

00:05:48,040  -->  00:05:50,150
so that's what it's gonna do it's gonna allow us
94

94

00:05:50,150  -->  00:05:54,090
to very easily perform a one item search
95

95

00:05:54,090  -->  00:05:56,740
in the database, just by using this method here
96

96

00:05:56,740  -->  00:05:58,940
and this is gonna be really useful later on.
97

97

00:06:00,550  -->  00:06:04,090
As a counterpart to that, we're gonna have find many by
98

98

00:06:07,560  -->  00:06:09,420
and all that's gonna do is exactly the same thing
99

99

00:06:09,420  -->  00:06:11,330
but instead of returning a single element
100

100

00:06:11,330  -->  00:06:13,603
it's gonna return a list of objects.
101

101

00:06:19,990  -->  00:06:21,640
Just like that.
102

102

00:06:21,640  -->  00:06:24,210
Now it's doing the same stuff but instead of finding one
103

103

00:06:24,210  -->  00:06:26,970
it's gonna find many and then it's gonna give us
104

104

00:06:26,970  -->  00:06:29,933
an object for each of the things that have been found.
105

105

00:06:31,750  -->  00:06:36,203
Now the last one that we're gonna add is the get by ID,
106

106

00:06:39,040  -->  00:06:44,040
and notice how I'm not calling this find by ID.
107

107

00:06:45,260  -->  00:06:48,380
Because the assumption is that when you have
108

108

00:06:48,380  -->  00:06:52,190
an ID property you are gonna have an element with that
109

109

00:06:52,190  -->  00:06:54,830
underscore ID otherwise it doesn't really make sense
110

110

00:06:54,830  -->  00:06:57,920
that you'd have that ID, after all if you don't
111

111

00:06:57,920  -->  00:07:00,160
have an element with underscore ID where did you get
112

112

00:07:00,160  -->  00:07:03,410
that underscore ID from in the first place?
113

113

00:07:03,410  -->  00:07:05,030
And so that's the assumption there, if you do have
114

114

00:07:05,030  -->  00:07:06,390
an underscore ID what you're going to do is you're
115

115

00:07:06,390  -->  00:07:08,633
going to go into the database
116

116

00:07:08,633  -->  00:07:10,800
and get the element that has that underscore ID.
117

117

00:07:10,800  -->  00:07:14,433
So we're gonna say return cls database.find one.
118

118

00:07:22,350  -->  00:07:26,483
Alright so you can see that this is gonna be like that,
119

119

00:07:28,010  -->  00:07:30,520
this is all it's doing is it's going through the database
120

120

00:07:30,520  -->  00:07:33,530
into the collection that we've got for this current class,
121

121

00:07:33,530  -->  00:07:36,910
be that item or alert and then it's going to search
122

122

00:07:36,910  -->  00:07:39,630
for underscore ID, it's gonna return an object.
123

123

00:07:39,630  -->  00:07:42,340
Now we already have a line that does something
124

124

00:07:42,340  -->  00:07:45,323
very similar to this, which is this one.
125

125

00:07:46,270  -->  00:07:48,560
It does exactly the same thing but you can define
126

126

00:07:48,560  -->  00:07:51,270
the attribute at the value that you want,
127

127

00:07:51,270  -->  00:07:54,570
so instead of defining this line ourselves
128

128

00:07:54,570  -->  00:07:58,440
we are going to return cls.find one by
129

129

00:07:58,440  -->  00:08:03,170
and underscore ID, underscore ID, why?
130

130

00:08:03,170  -->  00:08:05,860
Well I mean just that we have this method here
131

131

00:08:05,860  -->  00:08:07,340
so we might as well use it.
132

132

00:08:07,340  -->  00:08:10,170
If we do want to change the way that we interact
133

133

00:08:10,170  -->  00:08:13,270
with the database or maybe we wanna change find one.
134

134

00:08:13,270  -->  00:08:16,810
We wanna rename it into something else or whatever
135

135

00:08:16,810  -->  00:08:19,380
we only have to do that in one place.
136

136

00:08:19,380  -->  00:08:22,920
Because that's the method that's responsible for finding one
137

137

00:08:23,760  -->  00:08:27,163
so there we go this is how you should do that.
138

138

00:08:28,560  -->  00:08:31,250
Now that we have these methods we are ready
139

139

00:08:31,250  -->  00:08:33,430
to implement any other model
140

140

00:08:33,430  -->  00:08:36,490
and all we have to define really is just the collection
141

141

00:08:36,490  -->  00:08:38,370
and the data is going to be stored in it
142

142

00:08:38,370  -->  00:08:40,510
and what the JSON method is going to return
143

143

00:08:40,510  -->  00:08:44,220
and this superclass is going to deal with all
144

144

00:08:44,220  -->  00:08:46,950
the logistical issues of interacting with
145

145

00:08:46,950  -->  00:08:49,670
the database and you know finding things
146

146

00:08:49,670  -->  00:08:53,623
and things like that, so this is going to be really great.
147

147

00:08:54,960  -->  00:08:56,580
We're ready to move on to the next video,
148

148

00:08:56,580  -->  00:08:59,043
so thanks for joining me and I'll see you there.
