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