Nested Serializer | DRF

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] so in today's video we are going to see nested serializer okay so in order to create nested serializer we need two serializers okay as they have shown over here right and we need to compile this serializer down here and we are ready to go okay to see this documentation you can click on you know this nested serializer from API guide so first of all come to Django rest framework.org and from here click on API guide and serialize the relation okay and here you can find nested serializer relationship okay so here if you click on example then they have shown the example and here is the output right so we'll do the same thing so I'll just open my vs code now here before starting coding I'll just remove some commented code and this code is already available on GitHub okay so I'll just remove this this also I'll remove this function too and all this comment code now this seem to be clean code okay so if you remember well we have you know updated our model block model at that time I have created this category model 2 okay so we are going to utilize this now so I'll go to serializes.py here I will create categories serializer so I'll say class category serializer okay and here I'll say serializers dot model serializer okay and in here I'll say class meta and here model will be as a model equals to category I need to import category so I'll say category over here okay so category has been imported now and I will say pills um I guess I need to exclude sales exclude ID okay so this serializer has been created now I'll go to views.py here on the top I'll create class and here I am going to create category category list View and here I'll say API View and here I will Define get methods I'll say Dev get and here I will pass self request so I'll say self as cell comma request okay so here I will say all underscore category equals to category so I need to bring the category model over here also so I'll say category now here I'll say category category dot objects dot all okay so this will bring out all the category which is present in our database and after this we can pass this to serial ISO so I'll say serializer equals to category serializer I need to import that category serializer from the serializer.py so I'll say category serializer I'll copy this name and paste that over here okay and not comma parenthesis and I'll pass all underscore category and I will say many equals to true okay done now we need to return the response so I will say return response serializers I'll put s over here this is realizes.data foreign this should be fine so let's register this view to our urls.py so I'll just copy this name go to urls and here I will just you know comment I will say category urls and I'll say path same category underscore list put slash at the end and I'll say comma views dot category list view dot s underscore View don't forget to put comma over here okay but as it will throw you error and I will say comma name equals to category list okay this should be fine so let's go to browser over here I will again okay so we still have error what's that error okay I need to remove this uh extra field which we created while we were you know learning serializer model field now refresh okay everything is done now you can see that we have category implemented okay so they are showing the primary key because we have added for as a foreign key so if I go to models.py so here we can see category we have added it as a foreign okay that's why they are showing the um what we can say the category primary key field s okay don't worry we will fix these two okay but uh we will see nested serial as of for now so um after doing this I'll just close this models.py if I go to category underscore detail so I copy this sorry category underscore list if I go over here you can see I am getting this social media react.js and name okay so I also want that blog which belongs to this category should be displayed okay this is known as nested serializer so if I go to serializes.py and over here if I write category underscore name so I'll say I'm just creating variable Okay so I mean I go to models.py and here I'll say category underscore name equals to serializers dot character field okay and here I will say category equals to now I need to pass this block serializer okay block serializer and I will say many equals to true okay and I will say to read underscore only equals to true save this come over here refresh you can see we have these blocks now okay now here we can do one more thing like we need to change this variable we will say category underscore related underscore block okay this will make more sense right just refresh okay so why it's not taking so sorry we need to write this only yeah done this is because here if I go to block you can see we have this you know category related name is your category that's why right so we need to write category only over here just save and you can see we got this so if I go to admin panel redmi panel I'll just log in with admin credentials and if I go to blog and in this react.js if I change this to social media and save it then come over here and refresh then you can see react.js has no blogs but social media have to block now right so this is how it works and if you go to rapid API let me go to Rapid API where is it I need to install thing that I guess for now let us use this thunder client okay new request just send you can see we got this response right so I hope you got idea about all these nested serializer okay category detail okay so I will just close all these things now here I am just going to create one another view I'll just copy this paste below instead of this list I will write detail okay here in after request I'll say PK and instead of all I'll say get I'll say PK equals to PK so primary equals to primary key and we are not going to allow man equals to true because we are going to fetch one object only and our serializer is ready but I need to write single category okay and I'll copy this variable and paste that over here I will register this to urls.py file I'll duplicate this one here I will say category detailed slash int PK your it will be category detail View and here instead of the list I will say detail save it just right slash over here and if I go to browser refresh once now here instead of list I'll say detail and I will pass one slash okay so this is category detail page now you can see at the first primary key like if I open categories social media you can say social media has primary key one okay in url bar you can see over here so if I wrote one over here we got social media and we also got the blocks which are related to social media okay so this was the last things which uh last thing which I wanted to show you okay so now I will end this video over here so yeah this is it for today's video guys if you like my video please like share and subscribe to my channel and if you have any doubt please let me know in comment box okay I will definitely try to help you guys and I will see you next one thank you and yeah in next video we are going to see serializer relationships okay sorry API reference okay this is the API reference we are going to see all these things so I'll see you in next one thank you [Music] foreign [Music]
Info
Channel: Django World
Views: 2,789
Rating: undefined out of 5
Keywords: django, django world, djangoworld, django rest framework, django tutorial, django rest framework tutorial, django rest api, django rest api tutorial, django rest framework project, what is API ?, drf tutorial, django drf project, django reest framework for, django rest framework for beginners, nested serializer, nested serializer django, nested serializer in django, django rested serializer, nested serializer in drf, drf nested serializer
Id: SqwZuJHMq9A
Channel Id: undefined
Length: 13min 53sec (833 seconds)
Published: Tue Nov 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.