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