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

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ธ.ค. 2024

ความคิดเห็น • 87

  • @ajeetmishra816
    @ajeetmishra816 3 ปีที่แล้ว +2

    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 :
      th-cam.com/play/PLmDLs7JbXWNjr5vyJhfGu69sowgIUl8z5.html

    • @LakshmanKumar-qp9xn
      @LakshmanKumar-qp9xn 3 ปีที่แล้ว +1

      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 :
      th-cam.com/video/4dPVywV-X84/w-d-xo.html

  • @sagargahatraj5196
    @sagargahatraj5196 3 ปีที่แล้ว +3

    Thanks alot bro i was stuck almost three days and got burned out because of unknow about depth = 1 😭Thanks alot♥♥

  • @chris.dillon
    @chris.dillon 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว +1

      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.

  • @josiahdavid7735
    @josiahdavid7735 3 ปีที่แล้ว +1

    I'm loving this guy more and more

  • @phatpp1899
    @phatpp1899 2 ปีที่แล้ว +1

    Thank you so much, this video helps me alot !!!

  • @quangtranvan9606
    @quangtranvan9606 ปีที่แล้ว +1

    Very convenient, thank you very much

  • @danieldimitrov7067
    @danieldimitrov7067 3 ปีที่แล้ว +1

    Very useful info on Django, great work!

  • @amirhosseinebneroomi7280
    @amirhosseinebneroomi7280 4 ปีที่แล้ว +1

    good job man. I like your teaching style.

  • @sofistoyanova7121
    @sofistoyanova7121 4 ปีที่แล้ว +2

    Exactly what I was looking for. Thank you so much!!

  • @wirm9595
    @wirm9595 3 ปีที่แล้ว +1

    Hey Man, you've helped me out with this video. Thankyou, big time!

  • @frost1x322
    @frost1x322 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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 😅

  • @shrutiagarwal14
    @shrutiagarwal14 3 ปีที่แล้ว +1

    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?

  • @siddharthsrivastava4491
    @siddharthsrivastava4491 4 ปีที่แล้ว +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 ปีที่แล้ว +3

      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.

  • @oscarparedez1417
    @oscarparedez1417 3 ปีที่แล้ว +1

    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

  • @n.e.pranavakumaar4470
    @n.e.pranavakumaar4470 ปีที่แล้ว

    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?

  • @FullStackDevGreg
    @FullStackDevGreg 3 ปีที่แล้ว +1

    depth = 1 mind blowing for me thanks

  • @roja7854
    @roja7854 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว

      Could please share the code with me at:
      Codes.environment@gmail.com

  • @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)

  • @anilkumarmandava1165
    @anilkumarmandava1165 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว

      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:
      th-cam.com/video/nB1MczHlweA/w-d-xo.html

  • @chachacha1289
    @chachacha1289 3 ปีที่แล้ว +1

    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  3 ปีที่แล้ว

      Hello RC, please check the intro of this tutorial, it might help clear things up:
      th-cam.com/video/NCBxyw6rDds/w-d-xo.html

  • @dante7222
    @dante7222 ปีที่แล้ว

    Great tutorial tks. And if there are more than one car plan per car ? how can i setup the models ?

  • @dhanalm
    @dhanalm 4 ปีที่แล้ว +1

    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 4 ปีที่แล้ว +1

      I have also tried this
      books = BookSerializer(many=True)
      books is related_ name of author foreign key field in Book model

    • @codeenvironment
      @codeenvironment  4 ปีที่แล้ว +1

      Hello Dhana, can you please send me an email at: codes.environment@gmail.com
      Thanks

    • @dhanalm
      @dhanalm 4 ปีที่แล้ว

      @@codeenvironment please check your inbox. Thank you

  • @shubhamshirbhate9068
    @shubhamshirbhate9068 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว

      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 4 ปีที่แล้ว +1

      @@codeenvironment i try all the way still same

    • @codeenvironment
      @codeenvironment  4 ปีที่แล้ว +1

      Please email me at codes.environment@gmail.com,
      And I’ll have a look at your code

  • @Travellife8877
    @Travellife8877 4 ปีที่แล้ว

    osm video ........a big thumbs up

  • @amirhosseinebneroomi7280
    @amirhosseinebneroomi7280 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว +1

      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.

  • @ketankulkarni427
    @ketankulkarni427 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@codeenvironment thank you so much !!! It's helpful.

  • @saisarath4294
    @saisarath4294 3 ปีที่แล้ว +1

    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

  • @vinsmokearifka
    @vinsmokearifka 4 ปีที่แล้ว +1

    Thank you

  • @sameeraperera7912
    @sameeraperera7912 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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:
      th-cam.com/video/NCBxyw6rDds/w-d-xo.html

    • @sameeraperera7912
      @sameeraperera7912 3 ปีที่แล้ว +1

      Thank you again. I will try that.

  • @johnrobertcool
    @johnrobertcool 4 ปีที่แล้ว +1

    Can you please post edit and delete method?

    • @codeenvironment
      @codeenvironment  4 ปีที่แล้ว +1

      Do you mean a video for the [post, update, destroy] methods?

    • @johnrobertcool
      @johnrobertcool 4 ปีที่แล้ว +1

      @@codeenvironment Yes

    • @codeenvironment
      @codeenvironment  4 ปีที่แล้ว

      Django Rest Framework API #15 / Override Delete Action (destroy method).
      th-cam.com/video/Hi3TA2pGv7Y/w-d-xo.html

    • @codeenvironment
      @codeenvironment  4 ปีที่แล้ว

      Django Rest Framework API #14 / Override Create Action Inside ModelViewSet.
      th-cam.com/video/4dPVywV-X84/w-d-xo.html

    • @johnrobertcool
      @johnrobertcool 4 ปีที่แล้ว +1

      @@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 3 ปีที่แล้ว +1

    Request For update broo

    • @codeenvironment
      @codeenvironment  3 ปีที่แล้ว

      Hello Geri, please explain your request more , thanks.

  • @6957-c5k
    @6957-c5k 10 หลายเดือนก่อน +1

    You started your explanation from a very top-level, displayed code even before explaining what you wanted to achieve. smh

  • @yarik83men51
    @yarik83men51 3 ปีที่แล้ว +1

    +++