Friend System with Django (Send, Accept, Decline, Remove, Cancel)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the next part of the real time chat website in this part of the development we're going to build a friend system so that means everything from being able to search friends being able to then send friend requests also handling the different sort of statuses that can be involved you know after you send a friend request like if you go to someone's account what's it going to look like after you send them a friend request well there should be like a cancel button and if likewise if they sent you a friend request and you visited their profile there should be something that says like you can either accept it or decline it they'll also be like friends lists so we can view friends lists of users who you're friends with or you can view your own friends list there's going to be lists for friend requests because you could get multiple requests obviously and then later also we'll build a notification system that you can accept friend requests from so if you get a friend request a notification will pop up and you'll be able to either accept or decline that friend request within the notification itself and that's obviously going to be real time also oh and of course later when we work on the real-time chat system when we build private chat messages you'll only be able to chat with your friends so if unless you're friends with a user you can't start a chat or open up a chat that's going to be kind of i guess an added level of privacy that you'd want to add to it which makes sense because any other website like facebook you know you have to be friends with somebody to be able to send them a private message so as always when we're adding like a new feature to our website we want to do python manage.py start app and then whatever that feature or whatever that app is going to be called we're going to call this the friend app so pressing enter on that and that's going to generate a bunch of code for us i'm going to run the server right away because i know i'm going to have to eventually so i don't want to come back and do it so now the server is running and we've created a new app let's go into the kind of root level project directory go into settings.py and i'm going to increase this font size for you guys a little bit so you can see better and let's add that new friend app to our list of installed apps so now first things first let's press control s on this um i can leave this open i guess just so you know kind of what we edited in this video now we're going to go into models.py inside of our friend app and we're going to build that friend model the friend list model and also the friend request model so from django.com import settings from django.utils import timezone we will need the timezone later to set some dates now let's create that let's create the friend list app first a friend list models dot whoops models dot model now the first parameter in here is going to be the user every friend list obviously is going to have a user associated with it so essentially i'm creating like a primary key relationship to some user so the way i can do that is models dot one two one field so there's gonna be uh one friend list per one user that's kind of how you you would think of this so settings dot auth user model auth user model now i want to designate an on delete kind of criteria we want to delete all of the records so i want to say models dot cascade if a user is deleted it would delete then all of their that their corresponding friend list and before i continue i'm going to go up to view up here and wrap this word wrap so that it goes to the next line just so you guys can kind of get a better view so on delete models.cascade and now the the last field is going to be the related name the related name will be the user all right so the next field is going to be the friend list so this is this field will be called friends this is going to be the actual list of friends that they have so i want to do models.many to many fields to settings dot auth user model same thing so it's going to be a list of users basically you can think of it as like a list of primary keys i guess there's going to be one user that's associated with the friend list that's the one who owns it and then there's going to be a list of like primary keys to the accounts who they are friends with i want to set blank equal to true because it is possible that they have no friends and then i'm going to set a related name of friends the model is pretty simple that's basically all the fields that we need now we need our tostring function so this is what gets printed out if we just like access the model itself so string self return self.user.username i always like to just print out the username because some people like to keep their email private well most people probably like to keep their email private now add friend will be a function that we can execute on on a friend list and this is this is the second parameter here is gonna be the account that they're trying to that they're going to add as a friend so i'll write some comments here just so you kind of get a get a description of what's going to be happening add a new friend now the function is if not account in self.friends.all so we're getting access to that list of friends if the account is not inside of that account then i want to do self.friends.ad so i'm adding that as a friend and then just self.save so just a simple check are they already friends if they aren't already friends then add them that is the add friend function now the next one you probably guessed is going to be remove friend so remove friend self and account just like add friend oops add a colon there i'll write some comments just like in the add friend function and do remove a friend and scroll down to give you a better better kind of view so same sort of thing here if account in self.friends.all if they are friends then we want to remove them so self.friends.remove and remove that account you know actually looking back up it here i don't think we have to save it i think you can remove that so remove that or keep it if you like to be safe but it should be fine if you remove that this this doesn't require save i don't think so now the next function let's just go over here the next one is going to be unfriending so you can think of these two functions add friend and remove friend as like kind of the the core functionality there's not much checks like logic checks or things that need to be done it's just like strictly add this to a friend list remove this from the friend list now the next function is going to be unfriend so define unfriend now this is going to be a couple you'll need a couple a little bit of logic in here basically because you're going to have one user that is the remove e and one user that is the remover so one person that is executing the unfriend request basically like if i wanted to destroy our friendship i guess you would say that would that would mean that i'm the remover and the person who i'm removing is the remove e so that would be this person right here so i'll write uh write some comments here this terminology is a little weird but i can't think of like really better terminology than this so initiate the action of unfriending someone so let's like i said the the remover is the person initiating the action and the person who's being unfriended is the removie so you would go like remover friends list whoops friends list equals self and i'll write a comment here just to kind of clarify so per person terminating the friendship the person executing this you know destroying of the friendship i guess and then next would be remove friend from remover friend list so remover friend list dot calling that remove friend function that we just defined above we want to remove the movie the person who's getting removed see this terminology is weird but i think when you really think about it it does make sense now we want to do whoops do remove the friend from removie friend list so i want to access the other friend list so friends list equals friendlist and i have to look this up because it's not me it's some other user objects.get user equals removie i want to access their friend list and then i want to use that friend list so friends list and call remove friend and i want to remove myself so i could do like remover friends list dot user that would work or like if you wanted to simplify this you could also do self.user it's the same thing so that would be unfriending someone now the next function is going to be kind of like a utility function this is going to be for determining if we are mutual friends so is mutual friend self and friend and then just check you know are we friends is basically what this is going to be doing so is this a friend question mark so if friend in self dot friends dot all then we want to return true because that means we are friends otherwise just return false so there we have the friend list model object it's going to create a friendless table in the database and we have some utility functions for making things easier when we actually do all the friend adding the removing the unfriending all that stuff now the next model that we're going to build is going to be for a friend request so every time you you know go to someone's profile and you send a friend request this is going to create an entry in a database table so class friend request and models dot model i'm going to write some comments up here just to kind of clarify a few things for you i think that you'll find this helpful so a friend request consists of two main parts one part is the sender so that's the person sending the friend request person sending slash initiating the friend request friend friend request and the second part is going to be the receiver so receiver and this is of course the person receiving the friend request so now that we have that kind of clarification let's add some fields to this friend request model so we have the sender so one two three four five i'll do a bunch of tabs here do models dot foreign key so foreign key and do settings dot auth user model now you might be wondering why i used foreign key here and if you scroll up to the top here i used one to one field and if you were paying attention up here when i defined this user field i said a one-to-one field is basically like a foreign key but it's not a foreign key so how are they different how's a one-to-one field different than a foreign key and why did i choose to use a foreign key here and a one-to-one field up here so those of you who don't know anything about database design and sqlite let me just pause for maybe a minute here and explain the difference because i think it's it's definitely a worthwhile thing to explain if you already know what the difference between one to one and foreign key is just you know fast forward to when you see me on video writing code again so a one-to-one field when you use that in django what it does is you would have two database tables let's use the analogy of a car so say you would have say you're like a shop you're like an automotive shop and you have cars and you have a bunch of inventory of engines and you have a bunch of inventory of wheels i think this is a good analogy so each car you'd have a car table which would store all of your cars then you would have if you were using a one-to-one relationship you would have a table with engines and that that engine it has a one-to-one relationship with the car there's only one car and there's only one engine so there's a one-to-one relationship a car cannot have two engines it's not possible one engine one car that's what an example of a one-to-one field would be foreign keys are more for like a one-to-many relationship and and that naming that that terminology sounds confusing even though that's the terminology that people use when talking about foreign keys it's it's it's not really a one-to-many relationship but it it's a it's more like a potential one-to-many relationship and here's here's the analogy so if you have that car table you have a database table with all of the cars you have another table that holds um all the wheels so all the wheels that you have maybe you have a mountain of wheels at your own automotive shop you have a thousand wheels but each one of those wheels is tied to a specific car but you wouldn't use a one-to-one field because if you used a one-to-one field it would mean one car one wheel that's it so instead use a foreign key the the the wheels table each wheel entry would have a foreign key to a specific car so you see what i mean here by it's sort of like a one-to-many relationship but only in the potential each car could potentially be attached to any wheels any number of wheels even though there's only four it could be any number any you know wheels from the massive pile of wheels so then coming back to our django models we have a friend list now there's there's only ever one friend list per one user that's why this works with a one-to-one field there's one user and that one user has one friend list so it's a one-to-one field if you scroll down to the friend request one user could have could potentially send out any number of friend requests that's why this is a foreign key now the friend request object of course is tied to a specific user but that user could potentially send out any number of friend requests that's why it's more like a one-to-many relationship as opposed to a one-to-one relationship with the friend list with the friend list is one user one friend list all right hopefully you get it by now and if you don't too bad do some googling and do some research and look for some examples that's the best example you're going to get i think all right so now we want to we want to add an on delete strategy i want to do the same thing as we did with the one to one field uh models.cascade and then related name and then just set that equal to the sender so now i'm going to copy this and it's going to be mostly the same for the second field this is going to be the receiver again same thing foreign key on delete everything's the same except for the related name which is going to be the receiver the next field that we're going to write is is active this is going to be a boolean that denotes whether or not the friend request is active so a friend request becomes inactive when it's either been accepted or it's been declined so blank equals true null equals false and then default will be true so by default when this when this object when this friend request object or friend request entry is created in the database by default it's going to be true it's going to be active and of course we need a timestamp to denote when this thing was created so models date time field we want to use auto now add and set that equal to true so as soon as it's created it's going to create a timestamp entry now we need our two string functions so string self and then return self.sender.username like i said i usually return the username whenever possible now we need to build some utility functions for like accepting declining all that kind of stuff so first we have accept so define accept self and i'm going to write some comments in here so 1 2 3 and then tab twice over so accept a friend request update both sender and receiver so this is what this thing is going to do friend lists so if it is accepted then we want to you know add that friend to the sender's friend list and we want to add that request or add that user to the receiver's friend list so up here remember taking a look at our friend list object that means we have to query the friend list for the receiver query the friend list for the person who accepted or sorry the receiver and also the sender and then add each other to each other's friend list okay so receiver friend list equals friend list objects dot get user equals self dot receiver now i want to do if the receiver friend list is not none so whoops receiver friend list if it actually exists then i want to do receiver friend list dot add friend so just calling that add friend function that we just built earlier add friend and then do self dot sender next i want to do sender friend list so sender friend list equals friend list same kind of thing we want to query it objects dot get user equals self dot sender then we want to do the same kind of thing exactly the same sender friend list if it does not equal none then sender friend list dot add friend and then i want to add the receiver so add the receiver there we go and then self dot is active and set that equal to false and i don't know if you need to call save i'm going to call save just to be careful but i don't believe that's necessary next we're going to work on the declining function so coming up to the top just make sure i line this all up correctly define decline self write some comments just like we've been doing let me know if you guys like these comments i think it adds a nice little description to each one of these functions so decline a friend request and is it declined by setting the or it is it is declined it is declined by setting the is active field to false so remember that is active field up at the top here as soon as it's declined it gets set to false so that means it's no longer active it's been declined we are not interested in it anymore now i all i need to do is do self dot is active and set that equal to false and again i'm not sure if you have to call save here but calling it just to be careful now the last function is going to be canceling so canceling a friend request this is very similar to declining it's just going to be declining from the perspective of the person who actually sent the friend request decline gets called if uh like if you send someone a friend request and then they decline it whereas cancel is from the perspective of like me sending you a friend request and then i decide to cancel it so add some comments up here cancel friend request i can say it is cancelled i can do this in quotations i guess it is cancelled by setting the is is active feel to false so same kind of thing this is only different with respect to declining through the notification that is generated so right now there's going to be literally no difference between canceling and declining but later when we build the notification system it will matter because it's going to have different it's going to have a different way of execution and also a different notification that get that gets generated so for now it's exactly the same but later like i said we're going to be coming back and changing some of this so self.save even though i don't think you have to call that so there's our two models we have our friend request model which could be you know any user could send out any number of friend requests to any number of people and then we have a friend list which is a one-to-one relationship so there's one one user who has a single friend reque list and that never changes now one important thing that we do need to remember to do and hopefully i remember to do this in my notes i knew i do it at some point uh when we actually create a new user we should generate a friend list so we'll do that later i just want to kind of point out how it would be done so inside of a model inside of our account model we can actually use something called a post save receiver so as soon as this account object gets generated we can write a function down here which we'll do later like i said that will create a friend request entry in the database so i won't do that now i just wanted to mention it because it is very important and i hope i didn't forget to write it down in my notes later on when we actually do that so pressing ctrl s to save that now the last thing we're going to do is go to the admin for friend and update the admin file so that we can actually look at like the friend list and the friend request inside of the django admin so let's import our model so from friend.models import friend request and also friend list and now let's build the model for the friend list so class friend list admin by the way there's lots of different ways to build admins you can get like really granular with how you want your admin to look so i encourage you if you want to have any specific looks your your django admin to go and look up that information the django documentation is pretty good and there's a couple websites um that that have like really great examples actually you know what let me just go on a little bit of a you know 30 second tangent here while it's on my mind uh simple is better than complex is probably the best written django website that i've ever visited the examples and the articles that this guy vitor writes is like just awesome really really really high quality he doesn't have any video stuff but his articles oh maybe he does videos no not really he doesn't really have videos just articles they're like really really really high quality another great one is coding entrepreneurs so coding entrepreneurs or coding for entrepreneurs.com this is the guy this is the og guy who i learned django from i love him i highly recommend his stuff i have a membership on his website it's absolutely worth it his articles and he has tons and tons of videos so his his videos are very similar to mine too like if you like my teaching style it's very similar to to mine so anyway those are two websites total tangent let's just get back to this uh so list display equals uh this will be the same thing user i'm actually just going to copy this so this will be search fields and this one will be read only fields and then coming down class meta and do model equals the friend list and then i just want to register that so admin dot site dot register the model which is friend list and then the admin that i just built which is the friend list admin so everything has to do with the user so if you look at the model you look at the friend list model i'm not interested in like displaying the friends in the admin until you actually click on the friend list entry in the database so i'm just highlighting the user so the list is going to display the user every the search field is a user so if you search up in the top it's going to search the user read only field as user you should never be able to edit somebody else's friend list everything has to do with the user so next is the the friend request admin so class friend request admin admin dot model admin whoops that's not capital admin model admin uh let's do list filter equals the sender we have both the sender and the receiver that we can look this up by so sender and receiver i'm going to copy this two more times this is going to be list display and this one will be the search fields so that one center receiver that one center receiver now this one's a little different let me actually bring up the django admin um so the search field what that means is if i go here this search bar up here so the the search fields that i define here are what i can search up here so like um this this actually won't work because these are these are referencing account objects so if you go into models and you go down to the friend request these are referencing account objects so you can't search an account object you have to search a specific field within that account object so what you do to reference a specific field is you can do underscore underscore email or you know underscore underscore username so you're searching for that user based on either their email or their username and then of course if you wanted to search by their email and their username you could copy this and do email and username so that's what underscore underscore means it means you're going to select a specific field with that model so i'm going to copy this one more time paste it in and i want to wrap text to kind of give you a better view here and then i can do username so i want to search both the sender and the receiver by their email and their username inside of that admin now class meta and then just set the model some model equals the friend request and then at the end of the day dot site dot register i want the friend request object and the friend request whoops the friend what i don't know what happened there had my my cursor end up way over there friend request admin all right so there we go we're saving that that's all we're going to do in this video now because we've created a new model we've created some new database tables and we need to apply those changes to our database so python manage.py make migrations to generate the migration files for the things that need to be applied to the database now python pythonmanage.pymigrate to apply all of those changes so there we go now let's run our server again pythonmanage.py run server now we can go to our our admin for the development environment so coming on over here go to home and notice now we have kind of this little section for the friend app that we built we have friend requests and friend list obviously if we click on these there's nothing here because we've we haven't generated any rows in our database but if we did have friend lists and friend requests they would be showing in the admin now up next we are going to work on sending a friend request so if i was to visit you know someone else's profile let's just go to this guy's profile by the way i'm in the i'm on the real server openchat.xyz if i click this button that should send him a friend request so that is what we're going to work on in the next video hey what are you still doing here shouldn't you be on to the next video well since you're here i might as well tell you that if you're watching this on youtube shame on you you need to go to my website you need to go to my website because you get added benefits for watching the videos on my website and it's still free i get to track your progress and i give you all kinds of links in the description of the video that's going to help you get through the video it for every single file that i change in the video there's a link down below and it tells you what the specific code changes for those are plus i add you know added articles or other resources that i think you might find useful so if you're not watching on my website go to my website right now register an account it takes like 30 seconds there's a link down below in the description of this video go to the course and watch it there i'll see you in the next video
Info
Channel: CodingWithMitch
Views: 4,782
Rating: undefined out of 5
Keywords: django friend request, django friend list, django friend app, django friend system, django web development, django friends model, django friendship model, django friendship
Id: hyJO4mkdwuM
Channel Id: undefined
Length: 25min 19sec (1519 seconds)
Published: Mon Oct 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.