1
1

00:00:01,680  -->  00:00:03,540
<v ->In this video, we're going to be dealing with</v>
2

2

00:00:03,540  -->  00:00:04,900
databases in our code.
3

3

00:00:04,900  -->  00:00:07,813
We're going to be saving and retrieving our items
4

4

00:00:07,813  -->  00:00:09,760
from our database.
5

5

00:00:09,760  -->  00:00:14,760
What we want to be able to do is to get this item
6

6

00:00:14,960  -->  00:00:16,650
and save it to the database.
7

7

00:00:16,650  -->  00:00:19,665
We're gonna do something like save_to_mongo,
8

8

00:00:19,665  -->  00:00:20,990
and that's gonna save it to our database
9

9

00:00:20,990  -->  00:00:23,020
and store it there.
10

10

00:00:23,020  -->  00:00:25,210
Then we wanna be able to retrieve them,
11

11

00:00:25,210  -->  00:00:26,550
so we're gonna say items_loaded.
12

12

00:00:26,550  -->  00:00:28,583
It's gonna be Item.all.
13

13

00:00:29,580  -->  00:00:32,160
Then we're gonna be able to print them out
14

14

00:00:32,160  -->  00:00:34,963
or print the price of one.
15

15

00:00:35,940  -->  00:00:38,670
Items_loaded[0].load_price.
16

16

00:00:38,670  -->  00:00:40,510
This is what we want to be able to do,
17

17

00:00:40,510  -->  00:00:42,510
but of course we can't do it just now
18

18

00:00:42,510  -->  00:00:45,500
because we don't have the save_to_mongo method
19

19

00:00:45,500  -->  00:00:48,343
and we also don't have an all class method.
20

20

00:00:49,210  -->  00:00:51,840
Do note that we're calling this all method on
21

21

00:00:51,840  -->  00:00:54,840
the item and not on the item object,
22

22

00:00:54,840  -->  00:00:57,120
so this is the item class.
23

23

00:00:57,120  -->  00:01:00,680
In order to avoid confusion, I'm going to rename
24

24

00:01:00,680  -->  00:01:03,730
this item to something like iPad
25

25

00:01:03,730  -->  00:01:05,690
because that's what it is.
26

26

00:01:05,690  -->  00:01:08,330
So I've now renamed it.
27

27

00:01:08,330  -->  00:01:09,580
Says there's a typo there.
28

28

00:01:09,580  -->  00:01:11,550
There's no typo there, PyCharm.
29

29

00:01:11,550  -->  00:01:13,690
We've got this iPad object
30

30

00:01:13,690  -->  00:01:17,110
and the all method is being called on the item class.
31

31

00:01:17,110  -->  00:01:19,960
That's just to remove some confusion there.
32

32

00:01:19,960  -->  00:01:24,960
We have to go ahead and create our methods.
33

33

00:01:25,170  -->  00:01:27,190
Let's go ahead into our item model
34

34

00:01:27,190  -->  00:01:29,483
and we're going to make some changes here.
35

35

00:01:31,030  -->  00:01:32,890
The first thing we're going to need in order to
36

36

00:01:32,890  -->  00:01:36,930
interact with MongoDB is to know what collection
37

37

00:01:36,930  -->  00:01:40,610
the objects of this type are going to go into.
38

38

00:01:40,610  -->  00:01:43,560
If you remember, a collection in MongoDB is like
39

39

00:01:43,560  -->  00:01:46,920
a table in traditional databases to some extent.
40

40

00:01:46,920  -->  00:01:49,470
You can put anything you want in it
41

41

00:01:49,470  -->  00:01:52,060
and all of the things inside the collection
42

42

00:01:52,060  -->  00:01:55,300
should be similar in some way.
43

43

00:01:55,300  -->  00:01:57,540
What we're going to do in our app is
44

44

00:01:57,540  -->  00:01:59,970
we're going to put all our items inside
45

45

00:01:59,970  -->  00:02:02,820
a collection that we're gonna call items.
46

46

00:02:02,820  -->  00:02:07,193
So we're gonna do self.collection is items.
47

47

00:02:08,320  -->  00:02:11,610
That defines the collection to be items
48

48

00:02:11,610  -->  00:02:14,323
for all objects of the class item.
49

49

00:02:16,056  -->  00:02:19,910
As part of this, we're also going to add a repr method.
50

50

00:02:19,910  -->  00:02:23,930
What this does is, when you print an item object,
51

51

00:02:23,930  -->  00:02:25,380
you're gonna be able to see this
52

52

00:02:25,380  -->  00:02:28,320
string that this returns instead of
53

53

00:02:28,320  -->  00:02:31,093
the default string representation.
54

54

00:02:32,410  -->  00:02:36,963
We're gonna say something like Item self.url.
55

55

00:02:39,300  -->  00:02:40,920
Couple of things to note here;
56

56

00:02:40,920  -->  00:02:44,250
the F in front of the quotation marks means
57

57

00:02:44,250  -->  00:02:48,040
this is an F-string, also a new thing in Python 3.6.
58

58

00:02:48,040  -->  00:02:51,070
What it allows us to do is to put a variable name
59

59

00:02:51,070  -->  00:02:53,590
inside curly braces within the string,
60

60

00:02:53,590  -->  00:02:55,450
and what happens is that the value of
61

61

00:02:55,450  -->  00:02:57,530
the variable gets placed in here
62

62

00:02:57,530  -->  00:02:59,543
instead of these curly braces.
63

63

00:03:00,800  -->  00:03:05,620
It'll print opening crocodile clip, Item,
64

64

00:03:05,620  -->  00:03:06,900
the URL of the item,
65

65

00:03:06,900  -->  00:03:08,660
and then a closing crocodile clip.
66

66

00:03:08,660  -->  00:03:10,580
The crocodile clips are my addition.
67

67

00:03:10,580  -->  00:03:12,510
You don't have to put them in if you don't want,
68

68

00:03:12,510  -->  00:03:14,860
but you do need the F in front.
69

69

00:03:14,860  -->  00:03:16,510
Optionally, instead of using an F-string,
70

70

00:03:16,510  -->  00:03:18,763
you can use a format method if you prefer.
71

71

00:03:20,698  -->  00:03:24,153
So we have this, and we also need a _id,
72

72

00:03:26,520  -->  00:03:30,280
which is going to be stored in MongoDB.
73

73

00:03:30,280  -->  00:03:34,870
This is going to be uuid.uuid4().hex.
74

74

00:03:34,870  -->  00:03:37,230
Of course we do have to import UUID.
75

75

00:03:38,800  -->  00:03:40,360
What this is going to do is it's going to
76

76

00:03:40,360  -->  00:03:43,430
generate a unique string and place it
77

77

00:03:43,430  -->  00:03:45,310
inside this property.
78

78

00:03:45,310  -->  00:03:46,950
The reason for that is because then
79

79

00:03:46,950  -->  00:03:48,760
we're going to put that into MongoDB
80

80

00:03:48,760  -->  00:03:51,950
so that we can identify each item uniquely
81

81

00:03:51,950  -->  00:03:53,280
should we have to.
82

82

00:03:53,280  -->  00:03:54,400
At the moment we don't have to,
83

83

00:03:54,400  -->  00:03:56,523
but it's a good idea to put this in there.
84

84

00:03:58,450  -->  00:04:01,750
As well as the load_price method that
85

85

00:04:01,750  -->  00:04:03,900
we've got here, we're also going to need
86

86

00:04:03,900  -->  00:04:08,240
a method that turns the item from a Python object
87

87

00:04:08,240  -->  00:04:11,200
into something that can be stored in MongoDB,
88

88

00:04:11,200  -->  00:04:14,210
and that is a Python dictionary.
89

89

00:04:14,210  -->  00:04:16,233
We're gonna have a JSON method.
90

90

00:04:17,210  -->  00:04:19,060
This is going to return a dictionary,
91

91

00:04:20,070  -->  00:04:22,940
so we might as well tell Python that it's
92

92

00:04:22,940  -->  00:04:27,880
gonna return a dictionary, like that.
93

93

00:04:27,880  -->  00:04:30,510
What it's gonna say is _id is gonna be
94

94

00:04:30,510  -->  00:04:34,513
self._id, URL is gonna be self.url,
95

95

00:04:35,760  -->  00:04:37,427
tag_name, and query.
96

96

00:04:42,500  -->  00:04:43,870
That's everything we're going to store
97

97

00:04:43,870  -->  00:04:45,300
in MongoDB for now.
98

98

00:04:45,300  -->  00:04:47,060
This may change later on as we
99

99

00:04:47,060  -->  00:04:49,070
progress through the course.
100

100

00:04:49,070  -->  00:04:51,090
One more thing that we wanna change in here
101

101

00:04:51,090  -->  00:04:54,380
is that when we are loading an item from
102

102

00:04:54,380  -->  00:04:57,760
the database, we are going to extract from
103

103

00:04:57,760  -->  00:04:59,740
the database all of these properties,
104

104

00:04:59,740  -->  00:05:02,550
ID, URL, tag name, and query,
105

105

00:05:02,550  -->  00:05:05,120
and we're going to pass all of those to
106

106

00:05:05,120  -->  00:05:07,620
the item constructor so that we can create
107

107

00:05:07,620  -->  00:05:09,990
a new item object whose properties
108

108

00:05:09,990  -->  00:05:13,410
match exactly those that are in the database.
109

109

00:05:13,410  -->  00:05:16,000
However, at the moment, we're always generating
110

110

00:05:16,000  -->  00:05:19,240
a new UUID and we're never using anything that's
111

111

00:05:19,240  -->  00:05:21,430
being passed in to the constructor.
112

112

00:05:21,430  -->  00:05:24,530
So we're gonna change that by passing in
113

113

00:05:24,530  -->  00:05:26,780
a string constructor here that is going to
114

114

00:05:26,780  -->  00:05:28,273
default to None.
115

115

00:05:29,160  -->  00:05:34,160
What we're gonna do is, if this is not None,
116

116

00:05:35,280  -->  00:05:37,520
then we're going to use that value.
117

117

00:05:37,520  -->  00:05:39,900
If it is None, then we're going to
118

118

00:05:39,900  -->  00:05:40,990
generate a new one.
119

119

00:05:40,990  -->  00:05:43,130
This is a Python shorthand that says
120

120

00:05:43,130  -->  00:05:44,950
use this value if it exists,
121

121

00:05:44,950  -->  00:05:46,543
otherwise use this one.
122

122

00:05:47,880  -->  00:05:50,884
So that's a pretty handy way of doing things.
123

123

00:05:50,884  -->  00:05:53,670
That's all the changes to our item model.
124

124

00:05:53,670  -->  00:05:57,570
We are ready to go ahead and save to MongoDB.
125

125

00:05:57,570  -->  00:06:01,090
We can actually make the change that will
126

126

00:06:01,090  -->  00:06:03,930
allow us to put this data that's coming from
127

127

00:06:03,930  -->  00:06:06,903
this JSON method into MongoDB.
128

128

00:06:08,090  -->  00:06:10,830
But before we do that, we do have to go ahead
129

129

00:06:10,830  -->  00:06:14,660
and create a way of interacting with MongoDB.
130

130

00:06:14,660  -->  00:06:16,300
We're gonna do that in the next video by
131

131

00:06:16,300  -->  00:06:19,050
going for the first time into our common package
132

132

00:06:19,050  -->  00:06:21,620
and create a database module there.
133

133

00:06:21,620  -->  00:06:22,813
I'll see you soon.
