Django REST Framework Model Relationships

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone it's an the--from pretty printed here in today's video I'm going to talk about extending our API that we created in the last video using Django rest framework to have models that have relationships in them so basically one relationship is going to be a standard foreign key relationship so a one-to-many relationship and then another relationship will be a mini two mini relationship and you'll see how that's handled in Django rest framework but before I get started with the code I just want to remind everyone that you can go to pretty friend accom and check out the courses I have here most of the courses are free so you can check those out see what those are about and I'm always adding more courses or updating courses so make sure you check that out pretty per Netcom so what I want to do is I want to look at the code that I have already so if you recall from the last video I created a serializer for my language model and I have the language model here and then I created a language view to take in both that model and that serializer and of course I registered a URL for that view so you can actually see it in my API so like if I click languages I see the two languages that I have in my database so what I'm going to do is I'm actually going to update the model so instead of having a paradigm here it's going to be referring to a paradigm table so let me delete these two things first just so I can let's see number one delete and then let me go back to lists number three delete and the reason why I'm deleting is because when I migrate this new model that I'm about to create I would be left with some corrupt data if I did not delete those because it's going to be using the old model instead of the new one that I'm creating in just a second here so we're going to start in the models file and with the models file I need to add the two models that I want to add to make this example work so the first model that I'm going to add is something called a paradigm and that is simply a separate table for the paradigm here so obviously it makes sense why you would put the paradigm in the language class but for the purposes of our example here I'm going to put a paradigm in its own class and refer to adhere through a foreign key so the language class is going to depend on paradigm so I need to put it above and I'm going to call this paradigm if I can spell that correctly and here it's from model models top model and these are going to be pretty simple models so it's just going to have a name character field and then I'll just put the length is 50 again and I'll give it a stir method so you can see the name so that should be returned name and then inside of the language class instead of having the paradigm be models car field this needs to be models for in key almost and spelled out correctly takes in paradigm and then on the leaps it's going to be models dot well this should be equals models dot cascade okay there we go and then for the last model that I'm going to create I'm going to create something called the programmer class and as you can imagine this will represent programmers so like actual people writing code so the whole point of this is you're going to be able to know which programmer knows which language and then you'll know which language has which paradigm and for our purposes each language will only have one paradigm but in practice that's not exactly true a lot of languages can be more than one but we're just going to keep it very simple in this video so models table model and then I'll give this a name once again so all three classes will have names so I'm accidently equals 50 and then I'm going to create a mini two mini fields so I'm going to call this languages probably not the best name for our app but it shouldn't be too confusing so languages is going to be models dot Minnie - Minnie field and then I pass in the class where it has bad Minnie - Minnie relationship and that's going to be language and of course I will define a stir method and returned the name of the programmer so that's it for my model so now that I have that done I want to my or create migrations so make migrations and then I'm going to apply those migrations to the table so migrate okay so I have that done and now that I have everything migrated in my database I can go back to my code and after the model the next thing you have to think about the next thing in the chain is the serializer so the serialize ER will take the model and convert it into JSON forests or the other way around so I already have the language serialize ER here and what I'm going to do is I'm going to add two more serializers one for the paradigm and another for the programmers so I'll call this paradigm serializer and I'm just gonna use the same serialize er that I'm using for the language so the hyperlinked mono serializer which is going to put a URL in our results and then I need to define the metaclass the model is going to be equal in this case paradigm so I need to import that from my models file and then I need to import programmer as well so here the model is paradigm and then the fields I'll use an ID field the URL of course because it's a hyperlink model serialize ER and then I will also use the name I don't think there's anything more than that yeah it's pretty simple and then I'll create a third serialize er called programmer serial Lizer serializers dot a hyperlinked model serializer and once again is going to be very similar the model in this case is going to be programmer and then the fuels are going to be ID URL name and languages so this right here is where we're going to see something a little bit different so I'll explain that once we can visually see it here so I have my three serializers done so now the next thing I need to do is work on the views so I have the language view already so I want to add two more views I want to add a view for the paradigm and I want to add a view for programmers so I'll create one called paradigm view and I need to import things from the other modules so this is going to paradigm serializer that I'm importing along with programmer serializer and then from the models module or file I need to import a programmer and the other one is paradigm okay so the paradigm view is going to be view sets Top Model View set because I'm using this in a pretty standard way the query set is going to be paradigm objects da so just give me everything from the paradigms table for the query set the serialize or class serialize ER class is going to be equal to the paradigm serializer and of course the same process with the programmer view so few sets Model View Set query set is going to be programmer da objects all and the serializer it's so hard for me to type serialize ER for some reason but anyway serialize ER class is going to be programmer serializer okay so with those that is everything that I need for the views so I've created the new models I've created the new serializers I've created the new views and the last thing that I need to do is I need to create the new URLs and to do that I just register them on this router that I've created using default router so I already have one for languages I'm going to create two more one for a paradigm so I'll just call this paradigms so views dots paradigm view and then I'll do the same thing for the programmers so programmers and then views programmer view and that is it so not that many changes to add in new models and we'll see what happens when I go to actually run my app so run server hopefully I ain't making any errors okay everything looks fine so I'll refresh this page and now on my route I see three different URLs one for languages one for paradigms and one for programmers so to add in information I need to start with the one that has no dependencies so that is the paradigms I'm going to add a few so let's add functional let's add procedural and finally let's add object-oriented ok so if I go back to my list of paradigms I see one two three four functional procedural and object-oriented so now when I go to languages this is when we're going to see something that's a little bit different so I go to languages and I see I have a field to type in a name and then I also see I have this drop-down that was generated automatically for me to select a paradigm and the three paradigms that are that I can't select are the three paradigms that I just created so a functional procedure or object-oriented so if I do something like at C it put procedural hit posts that's done and then C++ we can say object-oriented I can do lists and say functional and let's add two more Java object oriented and let's see PHP let's use the old PHP so procedural ok so if I look at the language list I see C C++ Lisp Java and PHP but also with the paradigm because I'm using that hyper linked model serializer it gives me a link to the actual paradigm so if i click say the paradigm for a lisp it takes me to functional so that's the beauty of having the ID or not the ID but the URL for the parts that are in the relationship so now that I have languages and paradigms the last thing that I want to add our programmers so these are people who know particularly languages so I'm going to click that and you already see here in the form I have this multi-select box so by holding down ctrl and clicking I can select or deselect as many as I like because one programmer can know all five languages they can know one they can know none it just depends on the person so I'll put my name in first and let's say I know C++ and Java so I'll post so here is the object reflected back and you see the structure of it so I have the ID URL and name which are expected but then in languages it's an array of links and those links point to the languages that I have so if I click - I see C++ and if I go back and click four I see Java for the language so now let me add in a couple more programmers let's say Stacey Stacey knows C PHP and Java and then Zoe Zoe knows C++ and Lisp and then finally Billy knows just PHP so if I go to my programmer list and look here you see the structure of it so you see the languages that each programmer knows you have a link to those objects for the particular language and Stacey has three Zoey has two and Billy has one so as you can see this takes care of a lot of things for you even though you are using relationships in your model you didn't have to explicitly do anything with those relationships after you you created the model so once you were done with the model Django rest framework took over and was able to figure out everything for you even though you didn't have to specify anything in particular about the relationship you just have to say you know what fields you want it but other than that you didn't have to say anything explicitly about how the relationship is defined and that's the nice thing about working with Django rest framework like I was saying in the previous video if you want things to be you know a little more complicated or more custom for your particular use case there are ways to do that and perhaps I'll show you videos on how to do that but I I'm pretty sure that most people will get a lot of mileage out of the very standard case so you can use model view set and then for serializers you can use something like hyperlink model serialize ER or just the plain model serializer and that will get you what you want so that's it for this video like I said if you haven't been to my website pretty pretty calm check it out so you can look at the courses that I have there if you like this video please give me a thumbs up if you have any questions about what I covered in this video you can always leave a comment down below and if you haven't subscribed to my channel already please subscribe so thank you for watching this video and I will talk to you next time
Info
Channel: Pretty Printed
Views: 58,157
Rating: 4.9375 out of 5
Keywords: django rest framework, model relationship, django rest framework relationships, django, restful api, rest api, python, tutorial
Id: QB9gGEwxxM4
Channel Id: undefined
Length: 14min 31sec (871 seconds)
Published: Thu Mar 08 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.