Django Rest Framework API #16 / Many To One Relationship, Nested Data

  Рет қаралды 20,642

Code Environment

Code Environment

Күн бұрын

Django Rest Framework API #16,You Will Learn:
1- How to link two models using Many To One relationship.
2- How to get and show the nested data from the API response.
3- How to get the instance of the object that represent the nested data and save it by overriding the Create Action.
Source code: github.com/CodeEnvironment/dj...
Many To Many Relationship , Nested Data:
• Django Rest Framework ...
One To One Relationship, Nested Data:
• Django Rest Framework ...
Override Create Action Inside ModelViewSet video:
• Django Rest Framework ...

Пікірлер: 87
@sagargahatraj5196
@sagargahatraj5196 3 жыл бұрын
Thanks alot bro i was stuck almost three days and got burned out because of unknow about depth = 1 😭Thanks alot♥♥
@codeenvironment
@codeenvironment 3 жыл бұрын
Glad I could help 🙂👍🏻
@aalekhmotani3877
@aalekhmotani3877 3 жыл бұрын
me too thanks a lot code environment
@josiahdavid7735
@josiahdavid7735 3 жыл бұрын
I'm loving this guy more and more
@sofistoyanova7121
@sofistoyanova7121 4 жыл бұрын
Exactly what I was looking for. Thank you so much!!
@codeenvironment
@codeenvironment 4 жыл бұрын
Glad I could help!
@quangtranvan9606
@quangtranvan9606 Жыл бұрын
Very convenient, thank you very much
@danieldimitrov7067
@danieldimitrov7067 2 жыл бұрын
Very useful info on Django, great work!
@phatpp1899
@phatpp1899 Жыл бұрын
Thank you so much, this video helps me alot !!!
@frost1x322
@frost1x322 3 жыл бұрын
Great vids, I absolutely love it and think that it's best django tutorial on youtube. Yet, on small remark - you won't have to write all your model fields in serialization if you use *fields = '__all__'*
@codeenvironment
@codeenvironment 3 жыл бұрын
Thank you Denis 🙂, and yes you are 100% right about the fields and to be honest, a few weeks ago I was thinking about the same thing, like why I keep writing the whole fields lol, but I still didn’t find a reason for that🤷🏻‍♂️, maybe I’m not lazy enough 😅
@wirm9595
@wirm9595 3 жыл бұрын
Hey Man, you've helped me out with this video. Thankyou, big time!
@codeenvironment
@codeenvironment 3 жыл бұрын
I’m glad I could help ✌🏻
@ajeetmishra816
@ajeetmishra816 3 жыл бұрын
thanku so much this is what i actually looking for and did not find it anywhere
@codeenvironment
@codeenvironment 3 жыл бұрын
Thanks Ajeet, please check the Django REST Framework playlist , I’m sure you will find a lot to learn from : kzbin.info/aero/PLmDLs7JbXWNjr5vyJhfGu69sowgIUl8z5
@LakshmanKumar-qp9xn
@LakshmanKumar-qp9xn 3 жыл бұрын
Hey Ajeet Mishra.. Let me know how to write create methods in views..Table occurred 4 foreign keys... Please suggest me..
@codeenvironment
@codeenvironment 3 жыл бұрын
@@LakshmanKumar-qp9xn please watch the overriding create action tutorial for that : kzbin.info/www/bejne/apWzh6yti5KLbpY
@amirhosseinebneroomi7280
@amirhosseinebneroomi7280 3 жыл бұрын
good job man. I like your teaching style.
@codeenvironment
@codeenvironment 3 жыл бұрын
Thank you Amir 👍🏻
@chris.dillon
@chris.dillon 3 жыл бұрын
I know that this isn’t the forum for this and beginners don’t need philosophy... but postman is a GUI for manual testing. It won’t scale. It won’t catch bugs. I use postman instead of a browser I guess but I try to get away from it. The problem is that books and tutorials don’t focus on automated testing which is going to do many positive things for a project. In all tutorials like this, there is some section where the teacher asserts that the output is correct with their human eyes. This is not needed. A computer can read JSON. You can teach it what “right” is. Measure it with a non human sensor like science. Then tools and culture would move that way and it would be less mysterious how to build stuff that works, can be refactored, does not have upgrade regressions or more things like this that testing touches on.
@codeenvironment
@codeenvironment 3 жыл бұрын
Thanks Chris for sharing your thoughts, and I totally agree with you , therefore I’m preparing a tutorial for API testing. But for the purpose of understanding the basics, how relationships work and what the developers should expect to see as responses or how the request data should look like, postman comes really handy as keeping things simple helps beginners understand the concept.
@vinsmokearifka
@vinsmokearifka 3 жыл бұрын
Thank you
@Travellife8877
@Travellife8877 4 жыл бұрын
osm video ........a big thumbs up
@codeenvironment
@codeenvironment 4 жыл бұрын
Thank you :)
@FullStackDevGreg
@FullStackDevGreg 2 жыл бұрын
depth = 1 mind blowing for me thanks
@codeenvironment
@codeenvironment 2 жыл бұрын
I agree, it’s magical👌🏻
@shrutiagarwal14
@shrutiagarwal14 2 жыл бұрын
Super awesome! Query: I am trying to retrieve the list (in this example, car plan) to angular input, can you share any reference material for the same?
@oscarparedez1417
@oscarparedez1417 3 жыл бұрын
Hi Code Environment. I've got a question: How can I tell my serializer to only display certain fields from the foreign key reference? In this case just returning car plan id and car plan name?
@codeenvironment
@codeenvironment 3 жыл бұрын
You can use something called www.django-rest-framework.org/api-guide/relations/#custom-relational-fields
@roja7854
@roja7854 3 жыл бұрын
In apiview, am able to see all the details (including one toone related field details) but when I am trying to post the details there is no drop down field to set the other model object. I have included the all the fields including other model name in the serializer field attribute. Please help me on this
@codeenvironment
@codeenvironment 3 жыл бұрын
Could please share the code with me at: Codes.environment@gmail.com
@anilkumarmandava1165
@anilkumarmandava1165 3 жыл бұрын
Hi.. this is helpful. Can you also explain how we can display CarSpec details inside each CarPlan. In other words how can we see list of different CarSpecs available under each CarPlan in API
@codeenvironment
@codeenvironment 3 жыл бұрын
Thank you, one of the approaches is to add an extra field in the CarPlan serializer using a function that will get the CarSpecs objects related to the fetched CarPlan object. You can also use ManyToMany relationship in the CarPlan model to link multiple CarSpecs objects, see this video: kzbin.info/www/bejne/pHOUfpawfdGqm6M
@NguyenQuangHieu-IIOIO
@NguyenQuangHieu-IIOIO 3 жыл бұрын
what about one to many, how do i want the response to the carplan along with a list of the corresponding carspecs (sorry, my english is not good)
@siddharthsrivastava4491
@siddharthsrivastava4491 4 жыл бұрын
Plz anyone tell me this. This great but how can a user know that is id is for this plan while creating a new object.
@codeenvironment
@codeenvironment 4 жыл бұрын
Sorry for the delay, I was sick. You don't have to use the plan's ID, you can simply use the plan name while creating a new object, but before that you need to edit the create function in the views.py file: just replace car_plan=CarPlan.objects.get(id=car_data["car_plan"]) with car_plan=CarPlan.objects.get(plan_name=car_data["car_plan"]). Thanks.
@ketankulkarni427
@ketankulkarni427 3 жыл бұрын
@@codeenvironment and thanks a lot for the answer ! You are awesome.
@ketankulkarni427
@ketankulkarni427 3 жыл бұрын
Nice question.
@chachacha1289
@chachacha1289 2 жыл бұрын
Very interesting though you are not focusing on the main subject of your video which title is 'Many To One Relationship'. And it remains a little bit unclear for me. Tell me if i am wrong: One CarPlan can have Many CarSpecs. but: One CarSpecs can only have One CarPlan. So should'nt it be a One to One relationship ? If I have a class Car and a class User. A User can have Many Cars. But One Car can only have One User. If I set a models.foreignKey(User, on_delete=SET_NULL, null=True) on the car, don't I set a One to One relationship from the Car to the User? I am going to watch your video One to One relatshionnship, it may help.
@codeenvironment
@codeenvironment 2 жыл бұрын
Hello RC, please check the intro of this tutorial, it might help clear things up: kzbin.info/www/bejne/hHSlqayta9d3mtU
@n.e.pranavakumaar4470
@n.e.pranavakumaar4470 10 ай бұрын
Hi sir. I have a issue here. For me it is getting displayed as car_plan = null whereas in your case the corresponding id is rendered. How to solve this?
@dhanalm
@dhanalm 3 жыл бұрын
I have Author and Book models.In Book model, author is foreignkey. I want to display collection of books, for each author. So I have created property function in Author model and in Author serializer, I have added books in fields... But I'm only getting Book Id. I also want all fields of Book model. Please help me
@dhanalm
@dhanalm 3 жыл бұрын
I have also tried this books = BookSerializer(many=True) books is related_ name of author foreign key field in Book model
@codeenvironment
@codeenvironment 3 жыл бұрын
Hello Dhana, can you please send me an email at: codes.environment@gmail.com Thanks
@dhanalm
@dhanalm 3 жыл бұрын
@@codeenvironment please check your inbox. Thank you
@amirhosseinebneroomi7280
@amirhosseinebneroomi7280 3 жыл бұрын
I did the same thing I think, it gave me this error IntegrityError at /transactions/2 NOT NULL constraint failed: transactions_draft.office_id inside the draft object, there is an office object. and I want to create a new draft object. I pass the id of the office in the post request but it gives me this error
@codeenvironment
@codeenvironment 3 жыл бұрын
Hello Amir, could you please send me the code at: codes.environment@gmail.com I’ll check it out and get back to you as soon as possible.
@shubhamshirbhate9068
@shubhamshirbhate9068 3 жыл бұрын
hii.. when i use postman for post so it gave error 400bad request but i fallow you... so what i do.. for post request..
@codeenvironment
@codeenvironment 3 жыл бұрын
Make sure all the parameters you are sending are named currently and make sure you have a forward slash / at the end of your url
@shubhamshirbhate9068
@shubhamshirbhate9068 3 жыл бұрын
@@codeenvironment i try all the way still same
@codeenvironment
@codeenvironment 3 жыл бұрын
Please email me at codes.environment@gmail.com, And I’ll have a look at your code
@saisarath4294
@saisarath4294 3 жыл бұрын
I have created to nested serializer, but want to customize the nested output { Col 1: data Col 2 :data Nested data {col3 : data Col 4:data }} Required: {Col1:data Col2:data Col3:data Col4:data}
@codeenvironment
@codeenvironment 3 жыл бұрын
Please email me your Django App code for me to be able to help you more
@ketankulkarni427
@ketankulkarni427 3 жыл бұрын
How can i perform bulk update in Django Rest Framework ? Can you plz help me with it ?
@codeenvironment
@codeenvironment 3 жыл бұрын
Can you please explain more to me what are you trying to achieve in bulk
@ketankulkarni427
@ketankulkarni427 3 жыл бұрын
@@codeenvironment So I am fetching all the data from db against one table using Get api. Say there are multiple ids and 4 fields against each id. So for multiple ids, If user wants to update any of the field from any of the id, it should be done by bulk update. How should I achieve that ? I hope the scenario is clear to you.
@codeenvironment
@codeenvironment 3 жыл бұрын
Let’s say you have multiple items and the user wants to update the price for 4 items then you can get all the objects: Items = Item.objects.all() Then loop through the objects and set the values for price field Item.price = new value And outside the for loop you can do the bulk update: ITem.objects.bulk_update(items, [“price”])
@ketankulkarni427
@ketankulkarni427 3 жыл бұрын
@@codeenvironment thank you so much !!! It's helpful.
@sameeraperera7912
@sameeraperera7912 3 жыл бұрын
How we do one to many? I need to crate endpoint for ex- Order & order_details tables create using single endpoint Json- { "User_id" : "01", "Date" : "current date", "Address" : "address" Order_details: { [ "Item_code": "01", "Quantity" : 5 ], [ "Item_code" : "02", "Quantity" : 5 ] } }
@codeenvironment
@codeenvironment 3 жыл бұрын
Use ManyToMany relationship for that
@sameeraperera7912
@sameeraperera7912 3 жыл бұрын
Thank you. But how can I create both pedant and child table using one endpoint.
@codeenvironment
@codeenvironment 3 жыл бұрын
You have to override the Create / Post method in your view and send the nested or child data with your request . Check this tutorial: kzbin.info/www/bejne/hHSlqayta9d3mtU
@sameeraperera7912
@sameeraperera7912 3 жыл бұрын
Thank you again. I will try that.
@johnrobertcool
@johnrobertcool 3 жыл бұрын
Can you please post edit and delete method?
@codeenvironment
@codeenvironment 3 жыл бұрын
Do you mean a video for the [post, update, destroy] methods?
@johnrobertcool
@johnrobertcool 3 жыл бұрын
@@codeenvironment Yes
@codeenvironment
@codeenvironment 3 жыл бұрын
Django Rest Framework API #15 / Override Delete Action (destroy method). kzbin.info/www/bejne/fpqWhXRopaypbbs
@codeenvironment
@codeenvironment 3 жыл бұрын
Django Rest Framework API #14 / Override Create Action Inside ModelViewSet. kzbin.info/www/bejne/apWzh6yti5KLbpY
@johnrobertcool
@johnrobertcool 3 жыл бұрын
@@codeenvironment Hi I have one doubt can you help me to resolve that.stackoverflow.com/questions/64008694/multi-nested-with-django-restframework-and-mongodb
@geriarisman1621
@geriarisman1621 2 жыл бұрын
Request For update broo
@codeenvironment
@codeenvironment 2 жыл бұрын
Hello Geri, please explain your request more , thanks.
@yarik83men51
@yarik83men51 3 жыл бұрын
+++
@odiadavid6957
@odiadavid6957 5 ай бұрын
You started your explanation from a very top-level, displayed code even before explaining what you wanted to achieve. smh
@dante7222
@dante7222 Жыл бұрын
Great tutorial tks. And if there are more than one car plan per car ? how can i setup the models ?
Django Rest Framework API #17  APIView GET & POST Request and Response.
27:06
Django Rest Framework API #22 / Many To Many Relationship , Nested Data
31:20
Became invisible for one day!  #funny #wednesday #memes
00:25
Watch Me
Рет қаралды 52 МЛН
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 22 МЛН
Survival skills: A great idea with duct tape #survival #lifehacks #camping
00:27
THEY made a RAINBOW M&M 🤩😳 LeoNata family #shorts
00:49
LeoNata Family
Рет қаралды 29 МЛН
Django Rest Framework API #19 / One To One Relationship, Nested Data
25:33
Django REST Framework Model Relationships
14:32
Pretty Printed
Рет қаралды 67 М.
Adding Extra Fields On Many-To-Many Relationships in Django
17:45
Pretty Printed
Рет қаралды 42 М.
Django REST Framework - 1 - Custom User Model
24:06
rithmic
Рет қаралды 9 М.
Became invisible for one day!  #funny #wednesday #memes
00:25
Watch Me
Рет қаралды 52 МЛН