hey guys um in this video we'll be designing Instagram's back end in Django and we won't be doing all the entire features but uh we'll be doing most of the features like uh some of the basic uh building blocks of Instagram so yeah I think let's I mean the the flow I will be doing in this video is we will just we'll be discussing the design and we'll be discussing how the structure is going to look like and then we'll be discussing the database schemas and then we'll dive into coding so yeah those are the parts that I have decided to go with um yeah I think we can get started what I'm thinking is we can have one Django server running and we can have it one we can have the default DB um you can change it if you want but I'm going to keep it deep by default I think it's sqlite by default I'm going to keep that as it is like and I mean it's basic interaction between server and database and then we're going to have a number of clients and they'll be interacting with server this is the usual part the server and the client uh system so that's what the basic system is and then um coming to the design part so I have uh I'm going to like only go with the basic features of um Instagram uh so the core features of this is going to be post and the user because every other feature is going to be depending on these two models so you know post is a Instagram post you know you have the images and you have the text and all uh description and all and uh user is the authenticated user uh you know how it works and post comment is the comment of on the on the post so they're gonna be multiple comments and user follow is something uh we know like we the user follows some other user some other for other user follows this user something like that and we also have it model for table I mean model for likes so in order to track who likes who liked which posts and whose posts we have this so I have created a folder called insta and first before even creating a Django project what I'm going to do is I'm going to create a python environment because I want it to be like confined to this environment uh how do I do that insta yeah so I can do Source install bin slash activate and activate that so now you can see there is this uh environment active so whatever I can do now I can install it here so we have this folder this is the environments folder so I will do pip 3 or pip install Django and with along with Jammer will also be installing rest framework so we will be using this framework for most of the apis so now let's go with this framework so we have installed Django and this framework so now I want to create a project so I can see each angle admin uh start project I guess and the project is going to be Instagram and I'm going to Instagram and I can see I have manager pan this on the project so I can see python manager by startup so the app I want to start is let's say app itself the name of the app is app so now we have this folder app in which we have our application files um yeah I think so the first thing is we have to go to settings and add this app here so we need two apps here so one is the app which we created and another is a rest Frameworks app foreign because I mean if you see the documentation we will be seeing this so what I'm going to do is so we don't have urls.pi folder our file in our app so this is our app and this is the project so we have this urls.pi in Instagram which is a project but we don't have that in app so let's create that the reason being the application needs the URLs file right uh to root whatever API we need I'm going to say app slash urls.pi and I'm going to open the projects your urls.play and I'm just going to copy paste this and I'm going to add a new path here which is an empty path and I'm going to include app.urls and for include we need to import them so what this does is whenever we hit an empty path which is a slash basically so that um three directions to app.urls which is this file so right now we don't have anything here we'll be adding that um because we don't have any views so we can go into views so uh the structure that I want to follow is we want to have a views folder and that views will contain um each schemas we use so for user there is a separate view file and then for post there's a separate view file so that's what I want to do uh let me go into install yeah so what I want to do is I want to make directory inside app slash um yeah we can do views and I can say views slash app slash views slash okay I need two files let's see um we need button we will first finish the user.pi some of your refrigerators we have a views folder inside that we have um a user dot pi so user dotpad so if you want to import this we can do from App dot views dot user or you can say import user right so um let's so let's discuss about the DB schema so there is there are few tables as we were discussing so one is the user table the second is um the post table third is the post comment table for comments and fourth is a post like table so yeah I think that's most of it and we have user follow you have user photo or you can just say follow table inside user table we have the user ID which is the primary key and we have the email and we have the username we have the password we have the first name last name and when the profile or the user is created and when um he or she was updated so mostly that it can also have a bio which is an optional field so to create that we need to open app slash models.play we can create our models here so I want to see user this is the models so usually we'll do models dot model in a Django um when we're creating a Django model but what I want to do is I want to um you know input because the reason we are doing this is we already have a user model from Django by inbuilt right so we want to customize it according to our preference so to do that we need to inherit some class which Chango already has we can import abstract user you can see um it is a base class implementing fully featured user to username and password are required and other for other fields are optional so we are going to inherit this so we need because we are inheriting from a base class we need to add some by default uh variables like e-staff which is a Boolean field and is active to denote if the user is active ingredient field and is super user to see if the user is a super user or not super user is basically an admin access so we can add defaults to this by default we the user will not be a staff user and by default the user will be active we'll set that to empty and for every user we need a manager so manage it in the sense he will the manager is a class which handles whatever data this model is dealing with it manages the database so that's the manager so we can create a new file let's say app slash manager dot by so let's call this user manager and let's import that from App Dot manager import user manager and we can say objects is equals to user manager ring manager we need we are going to add two methods to this one is the create user method to create user takes username password another quarks and we'll also create super user foreign foreign foreign we are going to put our email as a primary key so let's say email is models dot email field and for username actually let's put username is none the reason being username by default is you know the primary key let's put unique true and username models.care feed unique will be true again username field will be email and we also have to change this in settings dot pi we will say auth user order will be app dot user this denotes that the default user is what we have in the models.pi and not the one Django has foreign we have two methods here in user manager so user manager also actually um you know inherits a class django.com trip dot auth dot base user we can import base user manager so yeah so first let's create a super user so this this method is called whenever we run the command python manage.create super user we will handle that here uh first what we're going to do is we are going to set default for uh for whatever values we want so we have is active set that to true copy that and E staff it is false and is super user is true so what are we going to do with this is that for the user I mean usually for this uh these fields we are setting the default and then we'll call this function which is a create user which creates a user and stores them in the backend or in the database so we are getting the staff user and if it is not true then we are raising an error and if Superuser is not true then then we are raising the error right and after that we are creating this um create group user and we'll send quags here so here we have this uh email so there are some processes before even storing the user uh if you have no like if you know like the password is hashed and stored in DB right and the email is normalized so um self dot normalize email so this non-message email address below lowercasing the domain part of it so it's basically like sanity check for emails the user is equals to serve dot model Dot create user email and password and quarks you can send email as email and we'll see user.set password of the password and user dot Save which says there to repeat and will return the user mainly there is a comma here this is what you want to do um so yeah so this is the basic user model we have the username um email and all actually this is a class right yeah so the first thing you want to do is we want to at least open this app and I will run this command make migrations so it says we have created a user model and we'll do migrate and it migrates um the model so whenever we are creating a new model we have to do this so you can see we haven't written others other fields like first name last name created and updated it I think we need bio but yeah so these are all like provided in the by default class in the abstract user so we don't need to Define that so I can also add bio here Garfield max length we can put it as 164 and we need to again migrate okay we can this is nullable is true so null is true so we can just migrate we have done that and uh yeah we can test this low so we can create the aps now foreign so to do that we'll go into the user.pi I'll just close this so yeah I'll just close this and this so in users user.pi we'll have all the um user methods of use so we the methods we are going to need are the apis for user model we're going to need is register login uh get user by ID right so this will basically get the profile of user and what else is there um so we need a API to follow and user follow unfollow so we'll need to do that so we are going to use the rest Frameworks uh we want to use the rest Frameworks class based views we have generic views so basically generic views what it does is it provides everything what everything we need like um we just need to imply uh you know inherit this and then we'll get what we need so I'll just explain what it is and I'll paste it here so we're going to have a class um so first is going to be create user and this is going to inherit create API View so before that we need to create something called a serializer the serializer is basically um to explain it simply let's say we have let's say we are returning a response of the user profile so it so we won't be getting I mean every time we need to return user profile we have the user model with us but we need to convert it into Json in order to return it to the client right as a response so to do that we don't need to create do it manually we can use a serializer to you know serialize and T serialize the data so you can see you can see delays from one data type to another and you can do the reverse which is known as T serialization and let's say you see it is Data from um model to Json and then you can then return the Json back to the user so that's what serializer does so in order to handle those serializations then we will have to create new file put the serializer dot pi so this is a separate topic for seat lasers actually so it's quite interesting so we'll just import the serializers here this is also going to be a class which is a user serializer and it's going to import clators.model C model serializer and this has to contain a meta method it's method meta class so we need to um say which all fields we need to serialize let's say for now all and the model which you are going to see a list which is a user we need to import that model so from App dot models import user so that's so the next is the fields which we want to see it is like when you are giving the model what all Fields do you want to see analyze so we want to serialize the email field which is CL address Dot to email field and we need to serialize the dot care field last name also seriously this is not careful and we have the username which is your letters or careful as well and we have the password which is a cad field so serializer does this and it also evaluates data it also can be used to create a model inside DB instead of doing it doing it like manually every time so for that we can do create it has a create method we can override it by saying create and the sending the validated data you can just say return user dot objects Dot create validated data We'll add this so what this does is when we do create method or when we do the save method in serializer it creates us an instance that's what this does so we can use it now so we have this create user I mean we can just first import this from app.serializers import user serializer right um and from App dot models import user model so for all we need to do is we need to define a query set which is user.objects.all and we need to define a Serial as a class in our case it's user serializer so that's all we need to do um so one more thing we need to do is we need to add URL for that right so we are going to add slash user we are going to add the user from app.views dot user we can import create user so if you do slash user slash create let's say hash create then it should create user for us so let's okay okay I think we need to we need to say um app slash views slash init.pi to declare it as a module and now we need to run the server app dot serializer I guess right foreign yeah we need to remove the Slash so let's try this I'm going to create a new user we don't need any authentication we need we don't need to know who the user is enrolled because we are just creating a user this is the post request and we have a raw Json input so we're going to have email let's say John at mail.com and we're going to have a username which is username or looking okay let's say John do and we have a password which is drawn one we have a first name let's say John we have a last name which is true let's say we have some bio here actually we didn't add bio but we'll add that this is a bio this is John's wheel right so we need we just have to add that the serializer we did not add bio View it's a cat feel let's try sending this so we need to send that to slash user slash create we have a runtime error it doesn't have his append slash let's try this and you can see we have uh we are getting this user back and we put the ID one which means it has created and we can actually check this by going into the shell manual dot by shell and let's import from App dot models import user and let's just call this objects.all and you can see we have this user and if you print that date and this is what we just created right so this is working we have one Apna which is a create user so I want to now we have registered user now I want to login so to do that we need to create another view so let's say login view so user login View or login user View so this time you're going to import something called API View so because login we have our own logic and all so we can't just Define this and automatically want want it to happen so you want to do something like create a token and all so for that case we need to in that case we need to inherit API view because it provides us some flexibility so for login it's going to be it's going to be a post request so I'm just going to say request right so for this um what we can do is the data will be coming from request.data and what we can do is we can serialize the data itself but I'm just thinking if it is required because um so basically how this is going to go is the user is going to send the email and the password we are going to verify it and then we are going to return I mean we are going to return the token for that we're going to create a token for the user and we're going to return that token right so that's what you're going to do so I'm going to take the email from request.data it's email and password also from request.data we can do some types of validation we can do that instead of serialize itself you know what we can create a new serializer user login serializer it's just a serializer so for this I'm going to have the email and the password so these are the two fields so now what I'm going to do is I'm going to initialize a variable serializer and that is going to be user login serializer of request.data now it is all validated and we can just say if serializer dot is valid to see if the uh request or the data is valid and if it is varied we will be getting the data inside serializer dot validator data right um so now I want what I want to do is I want to take the email I want to get the user from this email so let's say user is equals to user dot objects dot get email is serializer dot validated data of email if there is a user then what you want to do is I want to create a token for this user creating a token we need we can refer the documentation itself we have Authentication so in this we have token token Authentication actually we need to add this to the insta Labs first and then we need to import the token we need to import the token here so there is a condition also if I mean we can if the user is not phone then what happens easily get um get if object access you can check this okay so we can do that but what I'm going to do is I'm going to do try and accept I'm going to say object does not exist and if it does not exist we're going to return response here so in this I'm going to return success as false and the message is going to be used with it does not exist right if the user exists hello then uh what you want to do is I want to create a token so we can create the token foreign and if it all goes well then I will return the token is saying success is true and the token is this but before that there is one important thing to do we got the user we need to check if the passwords are valid so when we're doing Dot get we get uh the unhashed password but um yeah that we can compare it directly so if user dot password is same as utilizer.validator data of password only then create a token and return it else we are not going to return the token but we are going to say uh incorrect password we can test this um but we need to add it to the URL so path user slash login you can see what's the name EA it's login user View nothing user view dot as you foreign run the migrations thing because we have made some changes to settings as well I guess we don't think we need a migration yeah there is an aggression we can run this server we have this user I'll open another window here we have this user I'm going to say login it's going to be post request and the body is going to be in Json again and the email is John again just copy the email and the password we actually shouldn't return the password I mean yeah I could do that Korean try this we don't have a slash okay we in this um serial as if you have to send request.data for data is equals to and receive that okay token is not serializable I think it's token dot key right let's loot at least you have seen a token note key is what they are saying I can just test this one so this okay this is creating every time for the user token is only created once for one user so we can say get or create so if it exists it um just gets it or else it creates a token okay now it has no attribute key so I think we can just return the string format of the key of the token action okay it's actually to open up zero because I can see a tuple there oh or it's t of 0 dot key right that makes sense yeah so yeah this is a token we can use for any further requests to log in I mean go as John who is the token and yeah so we have created user and we are used we are logging in of the user I think we need to do some things like get user by ID right so we can just say get user we can use the generic views here to speed up the process we have autism views we have here we have retrieve retrieve API view it represents a single model instance we want a single user right so we can just say retrieve user and it inherits retrieve API View and it's the same thing there's really one change I'll just let you know that so in the path we need if you're seeing user slash then we need to specify apk here which is a primary key to denote the ID of the user so we will just do what's the retrieve user View retrieve user dot as you we could do that and if I try to so um yeah we can get the user that don't in order of any login for this so if I try this user slash one I should get the user with this right so if I try some other number we are getting not found yeah that is what we need so we have uh update view which updates the user will also implement this quickly these are just the crude operations here right so we can just say class update user and so this is going to be the same query soon but uh this is going to be different we get a P key here so the reason I'm doing put is that we want to update the user right I think we can do that directly um so it's he is updating his profile and not any others profile um so we need to check that for that first thing we need is authentication classes which is going to be a list and the first one is to open Authentication we can import that from this framework Dot authentication import token Authentication right and permission classes is going to be is Authenticator foreign s so we are going to override a method let's say put so the thing we need we are going to get a PK here which is the ID of the user I think we can just do this let's actually try this because we don't need a update thing so we can just say user slash update and then we get the update user as View so the thing I was thinking is that um if we need an ID of the we need to get the user who's logged in and we need to get the ID of the user from the path from the URL and then check if this use I mean we are updating this user but I think you don't need this you can just save the logged in user we can do put so what I'm going to do is I'm going to send the bio as this is an updated by right um so this is slash update so authentication details are not provided so the from the logged in token what we can do is we can go to headers and create an authorization header here and we can just say token space the token which we uploaded we have an error okay we need the ID of the um post so the user so let's say in PK here so it was saying it was asking for this it wanted PK and so okay let's test this actually one is a user which you want to update let's put the trailing slash as well so for update it's singing like this is the required this is all our required Fields right so for updating you usually don't need ID right you just need the current user which is a request.user and whatever we are updating foreign API View and we can remove the PK so serializer would be um 12. serializer class data is located close to data let's try printing the data okay we'll call the initial data so we haven't updated bio right so we want to update our serializer class I think we have something here okay we can send the the instance and the data which you want to update and send partial as true so we can see which is a user which is user is or we can just say request dot user and then the data and partial is true is it update no and then I think that should do yeah this will not save will update the existing custom rate so I will say if serializer dot is valid then I will just say see laser Dot save and I'll return a response foreign and else access is false and the message is error updating user will lock the error by seeing serializer Dot errors let's try this here it says user updated we have sent and we have updated the bio so let's get the user so we have user slash you can see that we have the updated bio here right yeah so yeah that works so in a similar way what we want to do is we're going to write uh delete user as well so the API View and we are going to have the same uh variables and here we are going to we are actually going to um put this destroy user because drf has a generic class called destroy API view I guess yeah so this has a built-in method I mean the Aldi method present call destroy now you want to destroy a user which means I want to destroy I want to delete an user from DB so I can either delete it or I can pay the market as not active so in this case I'll just delete it because if you have is active and then we have to add more um you know conditions to the query when you are getting like it should not be also is active so we don't want to get make it that complicated let's just keep it simple and so the conditions which you want to check before deleting the user is uh the thing we can just delete this right let's try doing that Swizzle slash delete and then delete or destroy foreign [Music] delete method I can just say delete the current user just delete okay um okay I'll just use this and do delete okay so destroy needs to have PK we can have apk here and we can check if PK is equals to request Dot user.id and if it is e equal we'll just say self Dot um destroy I think it's performed destroyed foreign try this and we need to add the PK here right and I'm going to delete one okay we are not returning a response so we can just do that foreign else we can return this same success as false and not enough permissions so we also have to check um if we use it even exists so if so let's say the user foreign catch for this same as this thing you can try Green Valley token because the user does not exist you can see the user does not exist and we have to register it again so the ID is to and then now we login we get the token and we delete it by updating the token okay we have to do two here the user is deleted and we get try to log in it should not be valid because it does not exist so we create again that it is three so if we do this again it's not great because the user already exists right so it throws us an error so if we do log in now it should give us a token I'm going to set this token because we need this for future values just update the variable token and save it okay so that's I think that's that for user but we need more features like follow user and all but we'll come back to that after now creating the post so this is done right um so we can go to post table so post table has an ID and it has it title it has a description and um created it updated it and the user who has created the table apis for this aps-4 post model is create crude on post and then like a post and um comment on a post we are now going to add replace to comments we're just going to keep it simple so let's first do the model thing so let's get the model out we have the user and now we have the post is going to be model should model the usual thing so ID is provided by Django itself we have the title which is cat field so the max length is going to be uh let's say 32. and the description is models.care fill max length is going to be 164. and created at models dot date time field and auto now add is true models.daytime field Auto now is true so the difference between Auto now add and auto now is auto novel usually changed once like when we create the model and auto now is true I mean if it is present then every time is saved this is updated so what we are updating is a daytime field so the date and time is updated for this column um so we have the user so the user is a foreign key right so we can say models.foreignkey and the model which we are referencing this user and we're going to set null is false because this can be null a post has to be created by user and on delete we're going to say models Dot set null so when the user is deleted this is going to be set null we don't want to delete the post when the user is deleted so yeah this is what you want it's migrate make migrations okay foreign foreign the migrations so we can now proceed with the serial lasers so I'm creating a post serializer which is a model serializer so for model seal this is class meta is important we will just paste that and this is post so again it's same so we're going to actually take this and paste it here created it don't need don't need and we need the natural models we want to see serial lasers will come to that so we can delete everything inside this so for user we can say serializers dot hidden field so the reason this is a hidden field is that when you are creating a post you don't need to specify which user is creating this because you're going to take that from um you know we're going to take that from request.user so in order to do that we create a hidden field so it is must to provide a default value for this so we can see um current user default so what this does is it takes the request variable and then puts the user here right so that's what it does in order to do this we need to send the context to the serializer which will be seen in a minute but yeah this so this is all we need for the posterior laser and for view we can open the post View I think it's you use you don't have a post view yet so app slash views slash post dot pi right so I'm going to import this so first we want to do the crude of post of class create post is create API View and um so we need the query set which is post Dot objects.all the sea laser class is post serializer so they should create a post and for URLs we need to add hit this from map dot views Dot post import create post so path post slash create Android slash create post dot as View yeah so we can try this right so we can try to create a post so to create a post also we have to check if it is authenticated or not so we need to add more classes here um so let's say authentication classes is token Authentication permission clances is authenticated you can just get that from user on these two yeah so let's try this let's say post slash create and this will be post and the body is Json and the title is post Title One description is post description one what else do we have we have yeah I think those are the two things we can but we need an header here so I'm going to add authorization header token is going to be the token variable so let's try sending this so we have this new post here so we don't have the user here let's because it's a hidden field you can go inside the shell and we can see um what the who the user is from update models and import post so I'll just say post dot objects dot all take the first one and give me the user you can see the user is uh whatever the token which we logged in John mail.com so yeah that is working and so we'll implement we need to implement the same thing to other URLs I mean other apis as well so we have created we need to get by get by ID uh get post which means retrieve post I guess we can take that from yeah I mean it's basically this and for urls you can just see post slash PK and we can see retrieve post oh this is slash one and that is a get request so for this we don't need to be authenticated so even if we remove the headers this should work because to view a post you don't don't need to be logged in right um so yeah this is done and we need to add two more I guess one is update post foreign we have used API View I think we can do the same we need to even check if the post is created by this current user we can just say API View um yeah so so this will be getting the ID of the post as PK now we need to add these things team and we need to add a method which is a put method 12 strand PK so with the PK we need to get the user or we need to get the current user and the PKS user so we need to compare the post so this is Peak is a post key so I mean post ID will get the post by saying poster objects Dot get ID is PK again if this post does not exist okay think we can use a Serial as a class this year laser is equals to post serializer of the post which we have done like similar to this the post actually let's do this like this uh post serializer Dot off post is equals to post or just post and data is our requested data partial is true so we need to find a way to because the model series we have this update function here right so we have this instance and violated data to do that we can what can we do we is it in conditional things in this okay it will be invalidated data okay um so if instance Dot user.id is equals to validated data of user then we can do self.perform how do we update this foreign this this will save and return so else it will not update and when we are saving if the serializer is varied let's say we can do serializer dot save and giving the user as request.user.id and then we can return a response saying success is true let's import this and the message is updated post else I need to return success as false foreign again you can do slash update slash the post ID um we can try this save and try let's create a new let's duplicate this we're going to have slash post slash update slash one slash this is going to be put request and we're going to update the post title this is an updated title right and the headers I think we need a header on that stand I think we have to put the update post here before to do that so if we do put here okay foreign user instance so foreign user okay we have updated the post if we get the post now so the title is updated and it was a bit tricky but yeah we have you have to do the delete one so let's do destroy post so this is same um but instead of put we have destroy so we do the same thing we get the object we get the post and then we create a seal laser we actually donate this but uh what does this this is seriously provide delete what else our test drive yeah foreign if it's the same then we can do destroy on the post and after doing that we can we can just do Post delete it uh else error deleting uh you can see um not enough permissions and actually put this into try and catch or accept and if the object does not exist then we see successes Falls and post doesn't does not exist now we try to delete this okay let's try to delete again we can have and we have to add a URL now so let's add delete which is destroy post and let's delete one PK is not defined foreign okay so okay I think we have compared the wrong thing it's the post Dot user.id the post is deleted and we try to get the post which is an art form it works so let's create a new post that's the body of the post yeah let's post this so yeah so this is a your post now and yeah it the crude functionalities of the post are done so we also need another API actually which is list every uh post off user to which means all the posts are created by the logged in user so it's a retrieve user posts so this will be trainer is there any retrieve we can I think there's a list okay I think this should have this should be logged in okay you don't need to be logged in but um there is the list method and what you what we can do is we can find all the posts or the user posts we're seeing post dot objects dot filter um user is equals to request dot user dot ID I guess we can return response access is true and posts is repos so there's a tricky thing to serialize the array of posts okay posts just posts that will list user what's the name we give retrieve user post so let's try that foreign post slash we can get the locked locked in users but yeah I mean this is like contribute because we have given request.user.id but we haven't given the authentication class so this is basically to display the profile right let's print what is there here foreign okay we can just use that right so we can just say serializer is equals to post serializer or self Dot C Laser Class of user pose and many will be true you can just say serializer dot data I guess or so if serializer is valid so this will be validated data else access will be false let me just delete this foreign data here um where is the series laser class okay it says foreign should you do something like this for a single one if you remove many then skill foreign [Music] just this I don't know if you can call data yeah we can go oh yeah we get we get all the user I mean all the um you know posts created by the user and if you log if you register with another user let's say John to John Doe two this is John toes bio two wheeler everywhere if I register this and if I log in with this user and if I use this token foreign that is working as expected right so we have gone through most of the apis for post now we want to create a like um we want to design a like a table so we'll create a new table for that let's say post like table so this will contain who liked which post for that um the post is required and the user who like this post is required only these two and we can do something like Creator control but we can ignore that for now so let's quickly create that post like which is model short model and we have a post which is models dot foreign key which is post and on delete models dot Cascade and we have user models dot foreign key user on delete models dot Cascade the primary key here is the post in the user which means the user cannot like the post more than once right so let's put class meta we have this unique together field here foreign post and user alright so we also set this to null as false right and here we can try migrating this python manager.pi make migrations and then migrate okay so um we need to create we can put this in the post um I'll only you will have a new class to like post View so this is an API View and in this we have um you get and to like a post you have to be logged in so we need these two classes and we get the post first of all by saying post dot objects Dot get ID is equals to PK and we'll put that in try catch we'll just copy that here right um so we have we have the post and we have the user which is requested out user and we need to say um let's put this here as a class as this posterior laser so we need to say that this user is liking the post and you need to create a new row in which the user is user and the post is this current post for that can we do um or can we just do Post like dot objects Dot create um so we can just create user equals to request.user post is equals to post right moving create if it doesn't access Django model you can see get or create what is guitar create two actually let's go to the Shell and post dot Object Store get our create through let's say title is Newton I think I can't do that here foreign then true okay we receive a tuple um let's see new post like so if new post like of one which is true or false if it is false it already exists if not then we will remove um new post like dot delete I guess so if we get just this can we call delete now we can call delete we will delete it and by deleting we're seeing that we have unliked the post so this is just post unliked else post light so I'll just test this once and I will explain um we have to actually create new post we have this ID so we will go to Gallery create an URL for this Oppo slash like slash this and we'll just call Post like I don't know if this works we have created this and we need just duplicate this and we need get and we're going to like the first post [Music] okay I think what did we okay we have imported the wrong one so it should be post like Dot okay propose does not exist I think we have four post light okay we have to and deleting it new post from zero dot delete unlike light and light and Light we can now check the from the shell from App dot models import post like post like dot object dot all the postage is fourth and user ID is three and the ID of this is three so I mean this is okay for small scale I guess because for our project this is this works so we have liked a post you have like or unlike or disliked anything we have done that and uh so to we can also view all the users in the post likes we can do that afterwards but first we need to finish this another big piece which is comments on a post so the Common Table is going to look like this so it's going to have an ID and there's a comment text which is actually a comment with written by some user and we're going to have a user field which is the which is a foreign key to end user and it represents who has written this comment and there will be a creator that field and an updated that field to see who has created when and updated the comment when all right so I think we can create that here itself so class actually we have to create the model first so it come a bit down and it'll say post comment that's model set model and there'll be ID automatically provided by Django so there's a comment text which is models.care field so we can put this max length at 264 bit higher and so the user is actually let's put the created at an update it here and now the user field who is the user model the foreign key to the user model is false because only logged in users can comment on a post and on delete now it will start Cascade so yeah we can migrate this now to manage dot by make migrations and now we can just make it and upload the migrations and so we can now create a a comment post so this is actually going to be create API View I guess so this is going to have the same classes like this and in this we are going to override the post um method we have the PK which is the primary key of the post which we're going to comment on or should we take that from the body let's take it from as a primary key the parameter only right um so we can we have the PK we'll get the post so we can just do this we have the post by using PK through the post exists then we have the user also with us so we are going to this is going to be we actually need to create the serial laser okay um so I'll just say comment serializer let's create that real quick and with this we need the meta class and we need just I guess two fields we need the common text and we need the user so this is going to be related primary key I think this is the same as this user in post I think we haven't put post here and yeah I think we can just use this so we have the post as well serializers dot primary key related field is true I think we need to migrate this again because we have made some changes okay we need to so we have added hmm can we do flush all the data okay because then Alice Falls here we need to specify some post value here so I will say four and at least make it so now we have the post with us and we have the model we have the serializer so we can serialize that we're seeing comment serializer and materialize a is equal to self dot serializer class and we want to create a comment so the data is going to be request dot data and so I think we need something more than this so we need to see which post the um comment belongs to so we can I think we can so usually what we'll do is we will just do Post comment dot object dot create um post is post and user is request.user right so this is what you do and we also do comment text is equals to request dot data of comment text this is how you usually do but we want to do this in a better way so what we can do maybe is so the thing is we need to add we can do one thing we can just say request dot data of post equals to post and then we can send the data as request.data as usual and if serializer dot is valid then we get the validated data [Music] and let's see commented else error adding a comment this is true I think this should be good to go and we need to add the URL as well so we'll come here this post slash comment slash PK and it is or have we named it common post now let's try this you have the header we'll do a post request to comment on Fourth post seeing this is a this is the first comment foreign so the serial is a field says it needs a context so we need to create the context actually we can just say by doing request is request and sending the context in the serializure class so this was quite tricky for me to find but I hope this works other than it is writing a comment okay so what is the errors what are the errors oh common text is required so okay this is comment underscore text and we have added the comment you can add it multiple times we can add I have added it three times so you can check in the Shell now I am actually exiting and again opening the shell because we have created new models and for that to take place we can just restart the shell we will say app dot models import post comment post comment Dot objects.org okay I think they forgot to do CS dot save foreign fail in post ID which means the post is not being sent with data so here we can override the save method here and we can just say uh self dot post is equals to quarks off post let's try this let's print quarks there is nothing there I think keyboard arguments come from uh when we are saving we just need to specify we need to specify the poster you don't need to do this maybe okay comment is added now and if we check it again it should be added and we have the common text we have the user ID and the post ID yeah that works and to get all the comments of a post we need another you can just to get here right so this returns all the comments in a post so the PK is uh is the ID of the post so let's actually copy this thing we need that for try catch we get the post and then we actually we don't get the post we get all the posts which has the post as PK let's do one thing we get the post and then comments is post comment dot object dot filter post and then um we need a serialism we need we'll use a Serial laser to do the same thing we can just do data I think we have done the similar thing here we will get returning multiple items I guess the user poster we have done the same thing we just said menu is equals to true we will just do that we'll just do request dot we'll just do Post oh sorry this is yeah so we'll do we'll remove context we'll put comments here and many will be set to true so if it is working and we can just send serializer.data error fetching post comments and I think this is a post common serializer okay you need to add the URL as well so post slash comments slash this gives us this is a kit right slash comments so we can just do get okay there's no data keyboard but if we add data here we weren't getting that right let's print comments also yeah we have okay we are getting query set but we need a list of items that's the same thing which we faced previously foreign [Music] I don't think so self dot serializer class a comments this is what we're doing we don't want to check is valid we can just do yeah and this is this should work yeah so we have all the comments for the post so if you do one post does not exist three there are no comments and four let me show all the comments yeah that works so we have let me open the tables yeah so we have done the apis for post as well like liking on a post and commenting on a post so there's one API remaining which is get the likes on your post so this should be post actually and we need to get so get is uh get the um all the people who have liked the list of um likes of the post right so let's do one thing let's pick this up and in get we can just paste this because let me remove the context first we'll get the post using the PK and then we'll we'll get the likes list from post like comment where the post will be post and then serializer also will use likes list and likes list we will send it and yeah so if this we should get from like slash four so this is okay we need a clear laser for post like as well foreign we have user and post right and we have the meta model is post like and here we will uh replace instead of self.c laser class we'll just say post like serializer let's see so the likes ID is this I guess it is the user ID right or is it like so let's like a post um pull slash like slash four is it post flashlight we have light I want to do that from another user let's create another user three three three three so we can log in now round three so let's use that to like where we have liked and if we get this the ID four and five the post is four right so the ID is four and five I think that's not the user ID post like so let's import that post like Dot objects.all the user ID we need the user ready um okay this is not a hidden field then yeah so we have the user so user three and five have like this post so I think yeah so we have the list of course so we have most features done so we have created the user let me remove this we have this follows remaining we have done a post like and we have done post comment um we have we know which user like which post we know which use a commenter on which post so we have to do user follow right so we have the follow table so one is the user ID so I mean the first one is the ID is created by Django the second one is user ID and the third one is follows ID so the user follows the followers ID user so yeah so they should be the apis for follow table follow and user to its follower and follow and user get list of followers right pull it us do that quickly here we have it follows post okay it's user follow s dot model so we have foreign and we need follows ID which is also a user and everything we can migrate okay let's add a deleted name SRC follow letter name destination follow it's basically some register name we need so the related name I think this is for how the other models see see this so the user model sees um this field as SRC follow oh yeah that's what related name is for so we have migrated and we need to create the apis we need to create the serializer first I guess so we have user follows hit follower follows follow serializer so this has the same user and follow foreign so yeah we can start creating the aps for this the first so this is user follow user this is going to be APA View so to follow you you need to you know you need to be logged in this is foreign this is user follow serializer and we have the first post and request so this is like three this is the following right so you follow and follow so we do that as post and we get all over so follow is typically saying we are saying that follow this user so follow stores the ID of the user we are going to follow so basically we need to get the user follow ing user which is user dot objects Dot get foreign except this so his user does not exist so what we need is we need to push the following or we need to create a Serial laser first see laser is equals to self Dot the laser class of we can do one thing or we can just say um user follow Dot objects.create user is request.user follow the name here follows ID I think this should be follows more than follows ID foreign and also this should be get or create so if not follow user of one which means on it only exists then we'll just say follow user of 0.delete else will say we have followed the user it's typical to liking I guess we return the response here followed user unfollowed user so let us try this so it's same as the URL we'll just put the it's follow slash would this be let's put it as PK um yeah so we can test this now so what we're going to do is we are going to duplicate this so we're going to follow the user with user id5 which user are we okay I think we can follow five with this user foreign so for API view previously we have done the like right with this one now we can do the same here user slash follow user isn't defined do we need to migrate that right foreign [Music] the user so I'm not sure what's exactly happening so we can check that here from App dot models import user follow so user follow Dot objects.org I think if we have run variables and this is unfollowed because we're deleting it so we have the user follow and we have the user ID follows himself but okay if we do four we have followed we use it again and this time user file is following four so you also want to get request .pk um so what this does is this Returns the list of users who the current user is following right so we can do that by saying um who the current user is following we can do uh following is equals to user follow objects.filter user is supposed to request.user who the user is following followers will be is reverse foreign and we can serialize this foreign so what we're doing is we are filtering out our list test this and let you know and explain what else is there the thing we don't need a PK here um let's do get to see if it works no let's take PK um we need a meta class we can do this user follow has no attribute post I think it's ready to do this um foreign attempting to get a value for field post okay user follow serializer okay okay this is not post light this is user follow okay we are following two people myself and four and myself follows myself so we have this data so we can yeah so I think that's most of the job we are trying to do we have this user follow uh class now so by using which we can know who is following who and we can also get the count as well so in scale if you are like let's say you have deployed this application and it's got some let's say fifty thousand sixty thousand users then you can't do then you can't count the number of follows each time uh because that's quite expensive then you can do something like caching to cash the number of count of followers and following for every user uh that would be a bit Central and you could do a few handful of things like that caching would solve a lot of problems for uh items in a small scale as well as big scale as well but very useful for small scale um yeah so I think that's what I wanted to do today we have the following model we have a common model and we have the like model so I mean it's I was actually thinking of doing the media thing as well but then the video would get too long so we didn't want that to happen so mediating is the same like you can just post you can just upload the media to S3 or something and then you can put the link in the DB so that should do the job and you can just get the link when you are from the front end when you are just displaying it to the user uh so yeah thanks for watching and uh do subscribe and share this video and if you have any doubts do comment um yeah thanks for watching