Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: The user entity with mongoengine

Login or purchase this course to watch this video and the rest of the course contents.
0:00 SQLAlchemy and MongoEngine have a lot of similarities and that's good because we've already set up everything in SQLAlchemy
0:06 and you'll see it's easy to do this as well in MongoEngine. So over here in the SQLAlchemy version
0:12 we have a users file, so let's go create one like that here. And then in this one we have a class. It derives from SQLAlchemyBase
0:21 so the table name has some columns. Let's just copy this over here and then work on it. So it turns out we're not going to need those
0:29 but we're going to need to import mongoengine. Now, how does this change if we move to MongoEngine instead of SQLAlchemy? Well, the Base class changes.
0:38 We're going to derive from a document. If you're doing embedded objects embedded within larger documents you can do embedded document here
0:44 but we're just doing a document. These are the top level ones. That part doesn't make sense anymore.
0:49 Now let's see, here we have a auto-incrementing primary key and this is basically an auto-generated ID in SQLAlchemy.
0:56 MongoEngine documents already come with a primary key that's set up that's kind of a good thing. It's called an ObjectId. So if we just omit that
1:05 it's going to create its equivalent of that. Now it gets interesting. Now we have a name and this is a column of type string.
1:12 How does this look in MongoEngine? We're going to come over here and say mongoengine.StringField like so. And here it's nullable is True
1:22 so we could say required is False but that's the default behavior. So only if nullable or False will we have to say required is True.
1:28 It's kind of opposite. But it turns out that these two are equivalent so just drop that line. And it's going to be pretty similar here as well.
1:38 So we have our string. Have this. I have some other things here nullable as true so we can leave that apart. Unique. It's True for this one.
1:50 And we're also going to have an index. Okay, so we can't put indexes in the columns we put them in a different spot
1:56 so I'm just keeping those as a little note right here. Let's see about this one.
2:01 All that's good, and I'm just going to remember that this requires an index. This one. This is a couple cool interesting features.
2:07 It's a Datetime. It has a default value and so on. So let's go over here and say this is a mongoEngine.DateTimeField
2:15 and guess what it also has a default just like this. And it's also going to have an index but not right there.
2:23 These last two we didn't use, so I'm just going to drop them. All right, looks like we're off to a pretty good start.
2:29 This is a little bit of wackiness here so let's do a little clean up. Now, we want to specify the indexes. We also want to control the table name
2:37 to be lowercase users. So we just do that differently here. So we have a meta dictionary. And over here instead of having a table we have a collection.
2:46 And this is going to be users, like so. And we have a db_alias. Remember, here where we say core. We want to set that here.
2:59 All right, this is how you select the connection it's going to use. Or even the server, and the database all that stuff. Okay, so we've got that.
3:05 And then final we want indexes. And this can be a list of either strings or dictionaries that contain, like, from composite indexes
3:14 things like that. So we want one for email we want one for hashed_password and we want one for created_date.
3:24 Okay, then that means we can remove these little notes. And this should be entirely done. Is it exactly the same as our other user? No.
3:33 These two are definitely different but you can see they're strongly similar. So is the way in which we query them and insert them, and so on.

Talk Python's Mastodon Michael Kennedy's Mastodon