Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAdam Cameron
Python Web Development Techdegree Graduate 16,731 PointsNot understanding what happened in this video
From the Relationship Model video: https://teamtreehouse.com/library/build-a-social-network-with-flask/how-to-win-friends/relationship-model
Can anyone explain in a bit more detail what kind of data is in the Relationship
model, how the indexes work, and how the query methods added to the User
model work?
What actually goes into the from_user
and to_user
fields of the Relationship
model?
Sorry my questions are a bit vague and open-ended but I've been following with almost no problem up to now and this one stumped the hell out of me. Would like to understand it clearly.
Sorry to bug y'all but maybe Chris Freeman or Kenneth Love could help?
2 Answers
Jacinto Jacinto
Python Web Development Techdegree Student 7,869 PointsI'm not an expert by any means, and it took a while for me to figure out what's happening in this lesson. I'll try to help you out.
The index is used to make sure there are no duplicates of the object. (So you can't follow the user twice, that would not make sense). You wouldn't be able to use the (Unique=True) like how it's used for the username, because you are dealing with 2 attributes tuples.
What goes into the from_user and to_users are Users models from the other table. That is why the ForeignKeyField is used. To allow the storage of User models to form User tuples in the Relationship model
The query methods were harder for me to understand, but I understand it now by thinking of the database like this. These are from_user and to_user examples (each object is divided with |):
from jeeno to kenneth | from kenneth to jeeno | from adam to kenneth | from jeeno to adam | from adam to jeeno
Just to make it clear from jeeno to kenneth means that jeeno is following kenneth ect. Let's use the "def following(self):" query method in this example and the current user will be "Adam". That method returns User.select().join(Relationship, on=Relationship.to_user).where(Relationship.from_user == self). Let's talk about the first part of the code which is User.select().join(Relationship, on=Relationship.to_user) This will retrieve all of the "to_user" data. If we use the data from above. It will look like this:
to kenneth | to jeeno | to kenneth | to adam | to jeeno
It's basically the same as the previous representation but without the "from" attributes mixed in. So now we understand that, let's think about the other half of the code "where(Relationship.from_user == self)". This will further filter the results. Since Adam is the current user (self), the data is then further filtered only showing the to_user data that is from Adam. The data base will now look like this instead:
to kenneth | to jeeno
Using that Query method, now we know all the Users that Adam is following, Kenneth and Jeeno.
J llama
12,631 PointsKenneth, don't you think you should be explaining these things a little bit better not just saying great answer??? you gave 0 explanation here, the db course doesn't go into anything this detailed? like what are you even doing ?
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherGreat answer!
Adam Cameron
Python Web Development Techdegree Graduate 16,731 PointsAdam Cameron
Python Web Development Techdegree Graduate 16,731 PointsThanks Jacinto, this was really helpful!
Richard Li
9,751 PointsRichard Li
9,751 PointsThank you for the detailed explanation!
Anthony Crespo
Python Web Development Techdegree Student 12,973 PointsAnthony Crespo
Python Web Development Techdegree Student 12,973 PointsThank you for the explanation very helpful!