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