1
1

00:00:01,650  -->  00:00:02,800
<v Jose>Hi and welcome back.</v>
2

2

00:00:02,800  -->  00:00:05,040
In this video we're going to talk about
3

3

00:00:05,040  -->  00:00:08,170
something you can do to prevent this warning here
4

4

00:00:08,170  -->  00:00:10,250
for self. _id.
5

5

00:00:10,250  -->  00:00:13,488
And also a small change to this line,
6

6

00:00:13,488  -->  00:00:17,270
in order to modify its meaning slightly
7

7

00:00:17,270  -->  00:00:19,990
while still keeping you safe.
8

8

00:00:19,990  -->  00:00:23,560
Or at least while helping you get rid of warnings.
9

9

00:00:23,560  -->  00:00:27,020
The reason why we put collection = models here
10

10

00:00:27,020  -->  00:00:32,020
is to tell Python that the model class and its subclasses
11

11

00:00:33,210  -->  00:00:36,466
will have a collection attribute.
12

12

00:00:36,466  -->  00:00:39,240
Now there is another thing
13

13

00:00:39,240  -->  00:00:42,110
that doing collection = models does
14

14

00:00:42,110  -->  00:00:47,060
and that is that if we have a model, such as alert,
15

15

00:00:47,060  -->  00:00:50,157
and we don't put collection = alert,
16

16

00:00:50,157  -->  00:00:53,510
whenever we save it to MongoDB
17

17

00:00:53,510  -->  00:00:56,920
it's going to go into the models collection.
18

18

00:00:56,920  -->  00:01:00,393
That's because the parent class has this defined.
19

19

00:01:01,410  -->  00:01:04,010
And, in some cases, that can be good.
20

20

00:01:04,010  -->  00:01:04,843
For example,
21

21

00:01:04,843  -->  00:01:07,900
if you wanna make 100% sure that you never lose any data
22

22

00:01:07,900  -->  00:01:10,890
even if you make a mistake in your application
23

23

00:01:10,890  -->  00:01:13,886
then you can have something like this.
24

24

00:01:13,886  -->  00:01:17,310
And then all your models that don't fit
25

25

00:01:17,310  -->  00:01:19,760
or don't have a collection property
26

26

00:01:19,760  -->  00:01:21,390
are going to go into this models class
27

27

00:01:21,390  -->  00:01:22,700
when you realise your bug,
28

28

00:01:22,700  -->  00:01:24,940
which you will do pretty rapidly,
29

29

00:01:24,940  -->  00:01:27,660
then you go and move them from the models collection
30

30

00:01:27,660  -->  00:01:30,670
into your alerts collection or somewhere else.
31

31

00:01:30,670  -->  00:01:31,560
However, there are some cases
32

32

00:01:31,560  -->  00:01:34,230
where you just want your application to crash
33

33

00:01:34,230  -->  00:01:37,130
if you forget to set the collection.
34

34

00:01:37,130  -->  00:01:38,500
And the way you do that
35

35

00:01:38,500  -->  00:01:39,418
is you can tell Python
36

36

00:01:39,418  -->  00:01:43,202
this property collection will be defined
37

37

00:01:43,202  -->  00:01:45,116
and it will be a string
38

38

00:01:45,116  -->  00:01:47,550
but it doesn't have a value at the moment.
39

39

00:01:47,550  -->  00:01:51,460
And the way you do that is you use type hinting to do this.
40

40

00:01:51,460  -->  00:01:52,893
Collection: str.
41

41

00:01:53,830  -->  00:01:56,031
And what this says is collection is a string
42

42

00:01:56,031  -->  00:01:58,663
and it doesn't have a value right now.
43

43

00:01:59,550  -->  00:02:03,364
If you then don't put collection = alert here,
44

44

00:02:03,364  -->  00:02:07,790
as soon as your try to use self.collection or cls.collection
45

45

00:02:07,790  -->  00:02:11,270
like we do here and here,
46

46

00:02:11,270  -->  00:02:13,732
then you will get a type error and say
47

47

00:02:13,732  -->  00:02:17,302
collection doesn't have a value, it is not defined.
48

48

00:02:17,302  -->  00:02:20,300
For _id, we can do the same thing.
49

49

00:02:20,300  -->  00:02:22,930
And the reason why we absolutely prefer
50

50

00:02:22,930  -->  00:02:24,380
to this for _id
51

51

00:02:24,380  -->  00:02:26,110
is at this point and time,
52

52

00:02:26,110  -->  00:02:28,245
we don't have an _id property
53

53

00:02:28,245  -->  00:02:32,030
and we cannot reasonably give one
54

54

00:02:32,030  -->  00:02:33,860
because the _id properties
55

55

00:02:33,860  -->  00:02:37,502
are created by the init method in each model.
56

56

00:02:37,502  -->  00:02:40,181
So what we wanna say is this is going to exist
57

57

00:02:40,181  -->  00:02:43,060
but we don't have a value yet.
58

58

00:02:43,060  -->  00:02:46,580
And this property here will be a class property.
59

59

00:02:46,580  -->  00:02:47,500
But you know,
60

60

00:02:47,500  -->  00:02:50,040
you can access them as instance properties as well,
61

61

00:02:50,040  -->  00:02:51,890
if each object has its own one.
62

62

00:02:51,890  -->  00:02:52,800
So this works
63

63

00:02:52,800  -->  00:02:55,541
for both instance properties and class properties,
64

64

00:02:55,541  -->  00:02:58,750
which means it is good for what we wanna do here.
65

65

00:02:58,750  -->  00:02:59,760
It gets rid of the warning.
66

66

00:02:59,760  -->  00:03:01,124
You still get the error
67

67

00:03:01,124  -->  00:03:04,410
if you don't define it in the subclass.
68

68

00:03:04,410  -->  00:03:07,323
So it's sort of a win win situation here.
69

69

00:03:08,355  -->  00:03:11,394
If you know Python from the past,
70

70

00:03:11,394  -->  00:03:13,260
and you've used abstract classes,
71

71

00:03:13,260  -->  00:03:14,790
you've used these meta classes,
72

72

00:03:14,790  -->  00:03:18,940
you may be aware of using the abstract property decorator
73

73

00:03:18,940  -->  00:03:21,923
to define abstract properties like these.
74

74

00:03:21,923  -->  00:03:26,500
It doesn't really get rid of the warning in many situations.
75

75

00:03:26,500  -->  00:03:28,790
And I'm not going to go into detail
76

76

00:03:28,790  -->  00:03:29,860
about using abstract properties,
77

77

00:03:29,860  -->  00:03:31,450
first of all because they are deprecated
78

78

00:03:31,450  -->  00:03:32,610
and you shouldn't use it anymore,
79

79

00:03:32,610  -->  00:03:35,796
and the new way of using abstract properties,
80

80

00:03:35,796  -->  00:03:40,796
which is using this abstract method decorator,
81

81

00:03:40,860  -->  00:03:43,430
still doesn't fix the warning that we've got here.
82

82

00:03:43,430  -->  00:03:46,960
So this is the best way to fix the warning,
83

83

00:03:46,960  -->  00:03:48,770
while still getting the error
84

84

00:03:48,770  -->  00:03:51,833
if you forget to define the property.
85

85

00:03:52,900  -->  00:03:54,280
Alright, that's it for this video.
86

86

00:03:54,280  -->  00:03:55,630
Thank you for joining me.
87

87

00:03:55,630  -->  00:03:57,080
I'll see you on the next one.
