Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Relationship Model5:36 with Kenneth Love
To make our app truly social, we need to add a model to represent the relationship between two users. We'll do this with a new model that has two foreign keys, both of which point back to the `User` model.
indexes- A list of indexes to create on the model. These could be fields to index for faster searches or, as in our case, fields to make unique. More information
.join()- A query that references another table or another query.
Okay we need relationships in our app so that we can actually call it social. 0:04 We'll do a pretty simple relationship where I can follow you, but 0:09 that doesn't automatically make you follow me. 0:12 The usual way of talking about this is calling it an asymmetrical relationship. 0:15 We'll do that with a couple of foreign keys and a new model, and 0:19 then we'll update our user model to add some handy query methods for 0:22 finding out who you're following and who's following you. 0:25 'Kay, so we need to build our relationship model. 0:29 And we're gonna add that in down here. 0:33 And it's gonna be called class Relationship. 0:35 And it's a model. 0:39 And so we're gonna have two fields. 0:41 We're gonna have the user that it's from which is always gonna be, 0:42 when you're thinking about this, me. 0:47 It's always the user who's logged in. 0:48 So from_user, and ForeignKeyField, 0:50 and up here we did this rel model user related name equals posts. 0:56 We can do that, or we can just say user and 1:00 then related_name equals relationships. 1:03 And then to_user, ForeignKeyField, also user. 1:08 And it has to have a different related name. 1:15 So we're gonna say related_to. 1:17 So this one you can think of, who are the people who are related to me, 1:19 and this one, who are the people I'm related to? 1:23 Now we have to add in something different down here to the class meta than 1:26 what we've had before. 1:30 So first and first we're gonna say database is equal to database. 1:31 But then the new thing is that we have to specify an index. 1:34 We haven't done indexes before. 1:38 So indexes tell the database how to find the data to remember the data. 1:40 But it also allows us to specify whether or not a index is unique, 1:47 so what we wanna do is we want to say that this is a unique index. 1:55 So let's go ahead and specify the index first. 2:01 So each index is a topple. 2:03 Inside of that is a topple of fields. 2:06 So from_user and to_user. 2:08 Those are our two fields that are in our unique index. 2:13 And then we say true, so that we know that this is a unique index. 2:18 So we're gonna save that, and we're gonna add relationship down here. 2:22 And then, we need to go change our user model. 2:29 Our user models queries, 2:34 these are gonna be some very different queries from what you're used to seeing. 2:36 So, let's come back up here to our user and let's add in two new ones, 2:40 one of which to get the people that we follow [SOUND], and 2:45 one of which will get the people that are following us. 2:49 So, let's start with the people that we're following. 2:53 So let's put a name on this. 2:56 The users that we are following. 2:57 And yeah, feel free to add doc strings and 3:00 comments to all of this stuff cuz there's a lot in here. 3:04 So return. 3:08 This is gonna take up a lot of lines, so I'm gonna stick it into parens here. 3:08 So user, and then, we're gonna say .select, and 3:14 then .join, cuz we need to do a join. 3:18 We need to select from multiple tables at once. 3:21 So Relationship, that's the table we need to select from, or the model, rather. 3:24 The field we're gonna select on is relationship.to_user, 3:30 so select where the one's that I'm related to 3:36 let's put that on the next line and then where, 3:42 relationship.from_user is equal to me. 3:47 Put that on the next line too. 3:51 'Kay, so. 3:53 We're gonna select all the users where the relationship to user. 3:55 All right, I wanna get the, the relationship.to_user, 4:00 I wanna get all those users where the from_user is me in the relationship, okay. 4:03 A bit of stuff moving around, like I said. 4:08 And then let's do followers, so these are the people who are following me. 4:12 Get users following the current user. 4:17 So again, this is gonna be multiple lines, so we'll do parenths, user.select.join. 4:25 Hey look, another join. 4:32 And we want to do relationship on relationship.from_user. 4:36 I'm just making sure I'm getting these, opposite to each other. 4:45 .where, relationship.to_user is equal to me. 4:49 And, yeah I'll put that on the next line. 4:54 So these two are basically exact opposites of each other. 4:56 So on this one we're joining on the to_user, 4:58 this one we're joining on the from_user. 5:01 We're doing a where for the from_user here, 5:03 we're doing a where on the to_user here. 5:05 So those are not amazingly [LAUGH] hard, amazingly hard queries to understand, 5:08 but if you've not worked with joins before, they're a little weird. 5:14 They're a little, I mean, it's almost ridiculous having to, 5:18 like, figure out where these things traverse. 5:20 Those queries are intense, but 5:24 learning to use joins like that will open up a lot of new worlds for using ORMs. 5:26 Now that we can follow people and see who's following us, 5:29 let's build the UI that we need to create these relationships. 5:33
You need to sign up for Treehouse in order to download course files.Sign up