Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Saving a user

Login or purchase this course to watch this video and the rest of the course contents.
0:00 With our user class defined let's see how we might use it. Now, we don't really have a whole flow put together just yet
0:06 so let's just go cram it into a place that it really doesn't belong but it will let us see how it works.
0:11 So, after this, we'll be able to create users and save them and even be able to create them from the database even though none are there yet.
0:19 So how would we go and work with this if we wanted to create a new user and save it? So, going on, say user, user.
0:25 I'm going to import this temporarily from the right one and then, we just set the values, like name is Michael
0:35 email is what my email is. That all looks good. We probably should set a password when we're ready to do that
0:41 but we'll go through the registration on that side and the rest has default values. So, once we've created this object, how do we save it?
0:48 Well, SQLAlchemy had this unit of work. We go get the unit of work, we create a session we add stuff, we do a bunch of work, and we call commit.
0:55 We don't do that in Mongo engine. It's active record style so each record can just directly be saved, which is simpler. Maybe not better, but simpler.
1:03 May or may not be, it's up to you. And this is it. That's all we got to do to go to MongoDB create the database
1:10 create the collection equivalent of the table set the effective implicit schema, enable the indexes and then, save this into it.
1:18 All this is basically done just by those four lines. So let's try again and see what we get. Oh, I guess it restarted itself once and already worked.
1:28 So, this is unique and it's pretty cool. We cannot have the email inserted twice so let's do, put something here.
1:36 I'll just do a save and the app crashed. Okay, that's fine. All right, well, let's just go look at the database
1:40 see what we've got. It should be inserted. In the second time when we say the bug is true Flask restarts this and that's why we're getting the error.
1:47 So, how do we look at MongoDB? There's a bunch of ways. I want to use this thing called 3T, Robo 3T. This is my favorite way to work with MongoDB.
1:56 Come over here, we're going to talk to localhost and notice, we now have a PyPI database that has just one collection for the moment.
2:03 We've only defined one entity. And here are indexes, like our email is unique and things like that. If we just go look at the documents
2:12 we should have a couple the two that successfully got inserted. The first one here and then, this is the second one that I made up.
2:19 Again, Flask, when it sees it, a debug flag it restarts itself in debug mode and so, that's why you got that error both times
2:27 but these are working right? These are getting inserted. How cool is that? You can even see the default values are working as well as the implicit id.
2:35 Now, over here, _id, just say that it gets added by the system, but in MongoEngine you just say .id. It sorts of omits that _ID.
2:45 Okay, so that's it. We've done a simple insert. Now, of course, it doesn't make sense to have this here at all, so I'm going to get rid of it.
2:51 We're going to rewrite this system so the entire website works with MongoDB but there we have it. Just make sure our app still runs.
2:59 Hey, look, now it's good. So, that's it. You can see it's really, really easy to use MongoDB and we want to get going.
3:06 I guess implicit in all of this is I already had MongoDB running in the background on my computer. Just Google how to install and run MongoDB.
3:15 It's pretty straightforward but there're a few steps you got to go through and it's not too bad. And just get the opensource community edition
3:21 run it, and then, that's it. You can go and run this code as well.

Talk Python's Mastodon Michael Kennedy's Mastodon