MongoDB for Developers with Python Transcripts
Chapter: Mapping classes to MongoDB with the ODM MongoEngine
Lecture: Required and default values in mongoengine classes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
So we are able to create a car and this is not a great architecture to just jam the writing here, but for now
0:07
we're just going to leave it right into our main application. However, let's go look at the car definition again,
0:12
there's a couple of things that would be nice, it's not part of MongoDB but it would be nice to require like we already have our type checking,
0:18
it would be nice to say that you have to specify a model and a make and it would also be nice to say you have to specify a year,
0:26
but maybe we could have the mileage default to zero, like 0.0 as a float for example
0:32
and it would be even cool to generate the vin id number here for new cars, right. Typically that comes with the car automatically
0:39
and you don't have to worry about it, you have to know what your id is.
0:43
So it turns out that this is super easy and this only is available in MongoEngine,
0:48
it is not available in PyMongo and it's not available in the database itself. So we can come down here we can say this is required
0:54
or must match a regular expression, or whatever, so we're going to say required is true,
0:59
so you must specify a model and given this is a Ferrari dealership we could either say this is required or we could give it a default Ferrari,
1:06
it's going to just make it required; the year also is going to be required so you have to type those three things in,
1:14
but maybe over here we could have the default be zero. New cars have 0.0 miles, that seems fair, how about this, how about auto generating that,
1:22
well, the default is really callable and we could just put actually like this
1:26
we could say float and it should call the constructor initializer for float we also put a value, so if we go up here, we can use uuid,
1:39
so if we import that, go have a quick look, you can use uuid4 as a string so if we say stir of this, we have that dash,
1:52
I don't think vin numbers have the dash so we could replace dash with nothing like this, what do you think that for vin number,
1:59
so if we could get this little bit of code to run on every time we insert a new thing, hey that would be cool, right,
2:05
and we can, so we go over here and say default, I would like to call a function that returns that value,
2:10
the simplest way is to do a lambda that takes no parameters and returns that, ok. So that's cool, let's actually wrap this around
2:17
so it fits a little better on the big font, small resolution, so now we have a better version, let's go back here
2:23
and now we can take away some of these things that we can just let it get generated and we'll save it
2:29
so let's try this one more time, so I'm a going to go down here and say add a car the model is F40 again, and Ferrari, the year is shiny new, 2017,
2:41
boom, notice it didn't ask me about the mileage or the vin number, but did that work, let's go find out, open the shell, turn it again
2:50
and look at the bottom one, check out the vin number, how cool is that. So we've got our vin number down here,
2:55
right this is the one I said 2017 not 2005, this was generated by our default value, this was generated by our default value,
3:02
and if I haven't done it yet, but if I for some reason omit setting the make let's see what happens if i don't set the make, remember it's required
3:10
it doesn't matter what I put here, boom field is required, make, right, we can't save it without setting the make
3:17
but we can save it without setting the vin number because that has a default. Okay, so go back here, so we can use required and default value
3:25
as well as other types of things like regular expression matches, these default values can be straight up values or they can actually be callables
3:33
which result in returning the thing that is the default value in this case each time we add a new car, maybe I'll show you, we'll get a new one,
3:42
it's going to be 308, and it will be a Ferrari, and it's going to be built in what is that, 1991, now if we go look one more time, there is a 308 again
3:53
totally distinct number or vin number here, right because each time we insert it, it's calling that lambda,
3:59
each time you call the lambda, you get a dramatically different uuid.