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

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

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

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

    love the demo on postman where we can see the final data structure and relationship of post and rates models!

  • @brenoverissimo3846
    @brenoverissimo3846 2 ปีที่แล้ว

    I was stuck in this part and you really helped me!
    Thanks!

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

    Excellent

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

    good one sir g .

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

    Amazing job. Congratulations and thanks for the amazing tutorial!

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

    Thx

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

    very help full

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

    Thank you for the wonderful tutorial.
    But I have a question regarding the update method,
    how can I update PostRate model along with Post model in same request.

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

    Thanks. Mostly we see OneToOne relationships in User - Profile. I use djoser to create a user object and signal to sync it with creation of a profile object.
    But you've shown an interesting example where we can use functionality of viewsets instead.

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

      Thank you. Always try to avoid over engineering when possible.

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

    Good video brother. I have a doubt. You created the PostRates model with 2 fields. Why not add the post as OneToOne field in the PostRates model? In that way we can query the PostRates model later using the post_id instead of going through the Post model. Which one will be a better design?
    Another doubt is that do you think splitting the Post and PostRates model is better than just adding likes and dislikes field in the Post model? What is the use of splitting them when it just increases the number of lines of code to save the rates data?

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

      Thanks Siddiq for the question.
      First doubt: you could do that, but then you will need to send two request to your API to get the post first and then the rates next and that is not a good approach especially when you have thousands of post. Plus you need more coding and effort in the frontend.
      Second doubt: adding likes and dislikes fields in the Post model is not wrong. but, splitting creates a better data and code structure ,helps with debugging and testing and makes your system more flexible cause you can link rates model with other models e.g( blogs, comments, articles ...etc) and actually you end up reducing the number of lines in your code.

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

      @@codeenvironment Thank you! Got it now. These design decisions come only with experience I guess.

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

    Hi it's very nice. But how to create more than one , for one to one relationship ..

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

      For example.. X class contains a, b, c, d, one to one relationship.. How to we have write create method in class based views... Please give me suggestion..

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

      @@LakshmanKumar-qp9xn You can just create an object for each relational field ( a, b, ... c) like what I did at 10:40 and then add them to the main object.