Building data-driven web apps with Flask and SQLAlchemy Transcripts
Chapter: MongoDB edition
Lecture: Package details cleanup
0:00 We've cleaned up the homepage.
0:01 Now let's just click on one of these
0:02 and see how amazing this is.
0:04 Oh, darn it, not so amazing.
0:05 Remember we had the relationship
0:07 from release over to package.
0:08 Well now we're running into the issue
0:09 but in the other direction.
0:11 We will go into a package and asking for its releases.
0:14 So we just need to change a little bit how this works
0:16 in order to match with our new data model.
0:18 So what we need to do is let's close these off
0:22 and it turns out that the error is actually right here.
0:27 We can go show you that
0:29 if we just scroll down and click right there.
0:31 It's this, it says, hey, we don't given a package
0:34 it doesn't have our releases.
0:36 Well, what are we really trying to do here?
0:38 We're trying to go to this and say
0:39 well, if there's a package, go get me its latest release
0:42 so we can set the latest release in the latest version text.
0:46 So let's just change how this works.
0:47 Let's say self.latest_release = package_service.
0:59 Like so.
1:00 Now, this function doesn't exist yet
1:03 but we're going to write in a second.
1:04 Now, let's just fix this up really quick here.
1:05 So the goal over here was to say, go to the
1:09 and get us the latest release that's already done
1:12 and then it's here's to say what is the text
1:14 for that latest one.
1:16 We probably want to add one more check
1:18 if there is actually a latest release, okay.
1:22 If we make this change and we write this function
1:24 this page, this package details page
1:26 should be back in action.
1:27 So let's go write this.
1:29 It's going to be package_id, which is a string.
1:32 What's going to give it?
1:33 What are we getting back?
1:34 We're getting an optional of release. Okay.
1:38 We want to do a query on releases, super easy.
1:40 We go to release, we say objects
1:41 and we don't need to use filter here.
1:43 We can just put it right inside of object
1:46 and say package_id equals package_id
1:50 that we're passing in and we want to do an order by.
1:55 What do you want to order by?
1:56 Let's go over here. created_date.
1:58 We want the latest one, remember?
2:00 So we say, - that and then here we say .first.
2:04 Give us one or None and that's it.
2:07 That will fix this or go to the database, do a query
2:09 get us the latest release for our package.
2:12 And here you can see we have an index on created_date
2:15 and the thing we're sorted on as well as the package_id.
2:18 So this should be really, really fast.
2:21 All right, let's go and see how this works.
2:25 Try again. No, what have we missed?
2:29 Oh we still are doing this test
2:31 even though it doesn't make sense anymore.
2:34 Try again. Tada, there we go.
2:36 Our site is fully back in action
2:38 and we can come down here and see all the details
2:40 here you can see we got our latest release for that one
2:43 on 2018 in May 30th. Super.
2:47 So we've now done a little tiny cleanup that we had to do
2:50 based on the change in the structure and the data model
2:53 and now our app is back and working and that's it.
2:55 We've converted this app over to MongoDB.
2:58 I've done a lot of talking and not that much typing.
3:00 If I just went and heads down
3:01 and started converting and converting
3:03 you can see this would go pretty quick.
3:05 We did come with the MongoDB data model already done
3:08 so I don't know. What do you think?
3:10 An hour, two hours to do this conversion.
3:12 That's not a ton of work and why is it not a ton of work?
3:14 Because we were able to change just these couple of files
3:17 'cause all of the data access was there
3:19 and because we're using MongoEngine
3:21 it was very similar in the way we wrote our queries
3:24 and got objects back
3:26 to the way that we work with sequel alchemy.
3:28 Super cool on both counts.