Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Post Model4:05 with Kenneth Love
To let users make posts, we have to have a model to store the posts in. We'll also want to add some methods to our `User` model to make fetching posts easier.
ForeignKeyField- A field that points to another database record.
Our post model is going to be really simple. 0:00 And actually very similar to our entry model from the databases course. 0:02 But then we'll set up some handy methods on our user model to make it 0:06 easier to get messages that a given user has posted. 0:09 'Kay. So we need to create our post model. 0:12 This is gonna be the thing that users can submit. 0:16 So these are like your Status Updates on Facebook or you Tweets on Twitter. 0:20 So, let's go down here and make a new class. 0:25 And we're gonna call this host. 0:30 And it's going to be a model. 0:32 Now, we don't need to use the user mix in on this because this isn't a user, so 0:36 we're just gonna do straight model. 0:40 So, we're gonna have a timestamp for when the post was created, date, 0:43 time, field, default equals datetime.datetime.now. 0:49 So just like when our user was created. 0:54 The user, which is a foreign key. 0:56 I have a hard time typing foreign key at times. 1:00 And we have to add a couple of attributes here. 1:04 Let's talk about these attributes. 1:06 So we add one here that's called rel_model. 1:07 And that's the model that this foreign key points to, so it points to the user model. 1:11 Notice we have the capital U down there on user. 1:16 And then we have related_name, and we're gonna call this posts. 1:20 So related_name is what the related_model would call this model. 1:24 So, if you're a user, what do you call the post model instances that you've created? 1:31 You call those your posts, right? 1:36 So, related_name is posts. 1:38 And then we need to create a text area to hold all the stuff. 1:41 And that's going to be a text field. 1:46 All right, class meta database equals DATABASE, 1:49 all caps, order_by is going to be negative 1:53 timestamp because we want to have the newest items first. 1:58 Now I wanna point out, we did this up here on users. 2:03 And I did it again down here on post. 2:08 Those have to be tuples and to be tuples they have to have a comma in them. 2:12 Now order_by could be a list if you wanna do it as a list instead of a tuple. 2:16 But if you're gonna make it a tuple, which I really think you should. 2:19 Because they're not going to be changed, they're not going to be modified. 2:22 Then you want to be sure and put in that comma. 2:24 The reason that we're doing the tuple with the comma instead of just a string for 2:27 an individual field is you can have it order by multiple fields, and 2:30 if there's a conflict, two of them came in at the exact same timestamp, 2:34 you could then have it sorted by something else. 2:37 Okay. 2:39 Let's change our user just a little bit. 2:40 We're gonna add a new method here called get_posts. 2:45 And we're gonna return Post.select().where(Post.user == self). 2:51 Now we could use the reverse foreign key 3:01 that we have where we can do like user.posts. 3:05 And that's probably a better idea, really. 3:08 But writing this get_post let's us see how we're doing this join thing. 3:10 Well not join but this, this query. 3:14 And then let's add another one here called get_stream. 3:16 Eventually this is going to be our posts plus the posts of people that we follow. 3:21 But for now it's just going to basically be exactly the same as get_posts. 3:25 But we're gonna structure it just a little bit different. 3:29 And you'll see what here. 3:33 We're going to do it here. 3:34 So post.user == self. 3:35 And notice that inside those parentheses so. 3:37 These are almost identical to each other. 3:41 But this is going to get changed later. 3:43 So, these two let us select the posts that we want. 3:46 So, these are all the posts that belong to a certain user. 3:50 Once we get our social graph set up, we'll be able to change that 3:54 get streamed method to gets posts from users you followed too. 3:56 I can't wait. 3:59 Before we get there though, let's set up the form and 4:01 the view that we'll use to create a new post. 4:03
You need to sign up for Treehouse in order to download course files.Sign up