Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Models6:41 with Kenneth Love
Flask-RESTful's approach is to create resources and then define methods on them that relate to routes and HTTP verbs. This may seem simplistic but it's very intuitive and handy for quickly standing up an API.
review_set for the
Review model is actually redundant. If you don't provide one, Peewee automatically creates a back reference like
<lowercased model name>_set for you.
Flask makes it really easy to build routes, control HTTP methods and 0:00 headers and pars and send out JSON. 0:04 But why write all of that ourselves, we don't have to, right? 0:06 There's an excellent package named Flask-RESTful that we're going 0:10 to use to save ourselves some work. 0:13 With Flask-RESTful, you create classes, kind of like models, that represent your 0:15 resources and the HTTP methods that you want to allow on those resources. 0:19 We're gonna spend a lot of time working with these resources, so 0:24 let's get started right away. 0:26 Okay well, the first thing I need to do is, install the necessary packages. 0:29 So let's do pip install flask-restful and 0:33 then, I need an ORM, so I'm gonna install peewee as well. 0:37 I don't have to install Flask, 0:42 because it's automatically installed on workspaces, so that's great for me. 0:43 If you're working along at home, you'll need to install Flask as well. 0:49 If Flask-restful doesn't already install it for you, I have a feeling it does. 0:52 All right, so now, I want to make my basic app and my first models. 0:56 I wanna be really creative, so I'm gonna call my app app.py. 1:02 All right, easy enough and then, let's go ahead and we'll make our normal app. 1:06 Flask import flask and 1:13 then let's debug equal to true, host equal to 0.0.0.0. 1:16 Since we're on workspaces we need to be looking at all the ports and not just or 1:22 not, sorry not the ports, all the interfaces. 1:26 And not just the looped back local host interface and then, port. 1:28 Why do I have available here? 1:34 We'll do 8000, all right, so port is at 8000. 1:35 And then we'll make Flask(_ _name_ _), paying attention to the current file. 1:41 And then let's add a route to this, just to be certain that things are working. 1:46 And we'll just do addl loop at that forward slash,def hello_world and 1:57 we'll just return 'Hello World'. 2:00 All right, now let's get down here and if the name is equal to main, so 2:01 if we're running the file directly. 2:06 Then we're gonna do app.run debug=DEBUT, 2:09 host=HOST and port=PORT. 2:14 While I'm thinking of those, I'm gonna change my spaces to 4 and all right. 2:18 So I'm gonna save that, I think we're good there. 2:24 I'm not gonna even run and test this right now. 2:28 Actually, I will. 2:32 Let's go ahead and do that, so we'll do python app.py and 2:33 then we'll hit here and go 8000. 2:38 And cool, we got Hello World, great. 2:41 All right, so now I wanna make my models. 2:45 I'm gonna make another new file models.py and then let's setup these models. 2:47 So we're going to import datetime and then from pewee import, 2:54 I don't like doing that but that's the pewee standard. 3:00 Database = SqLiteDatabase and I'm wanna call this courses.SqLite, 3:05 you can call this whatever you want of course. 3:12 All right and then make a course model and 3:19 we'll give this a title, that's a CharField. 3:23 And we give it a URL, that's a CharField and I'm gonna make this one (unique=True). 3:27 So, that people don't submit the same URL twice, hopefully. 3:32 I mean, you could maybe use a forward slash at the end of one of them and 3:35 you don't at the end of the other one. 3:40 But close enough, And then we will do a created_at,. 3:41 Which is a DateTimeField and we will set a default in here of datetime.datetime.now. 3:45 And we don't put the parenthesis, because we want to call the function. 3:51 Whenever it creates the field and not whenever it instantiates the model. 3:56 And we'll do class meta and database=database. 4:02 All right, so pretty straightforward model. 4:07 This is some of the other models that we've made in the past and 4:11 let's make one more here. 4:16 Class Review which is also a model and 4:18 course = foreignKeyField. 4:23 And this is related to course up here, and related name, 4:27 I'm gonna call this review set. 4:32 And rating is an integer field, 4:37 comment is a text field with a default of an empty string. 4:42 Basically makes a thread, they don't have to put in a comment. 4:47 We'll just record an empty string if they don't provide one. 4:49 And then again, created_at is date time field, default is datetime.datetime.now. 4:53 And that's mainly just for housekeeping, so 5:00 that we can see when things were created. 5:04 Database = DATABASE. 5:07 And then let's set up an initialize function and 5:10 we'll do DATABASE.connect, DATABASE.create_tables. 5:15 And I wanna create course and review. 5:23 We'll have this be safe, so those two tables already exist, it just passes over, 5:27 it doesn't try to throw an error or anything. 5:30 And then database.close, all right. 5:32 Cool. 5:37 So two models, title and u.r.l created for that course and 5:37 course rating and comment and created_at for review. 5:42 Nothing new or out of the ordinary there. 5:46 Now I need to make sure that the app 5:49 makes sure that the models are created whenever the app starts. 5:52 So let's quit the app real quick, 5:56 control C and then, let's come over here. 6:02 And up here at the top, we'll add in import models 6:06 and then down here in the name main, 6:12 let's have it do models.initialize. 6:15 Okay cool. 6:21 So it will run this function whenever the app starts or 6:23 it should but let's make sure of that. 6:27 So, I will run that and if I refresh my column over here. 6:31 Yeah there we go, courses.sqllite. 6:37
You need to sign up for Treehouse in order to download course files.Sign up