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

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ส.ค. 2024
  • Django Rest Framework API Model Relationships
    How to implement ManyToMany Relationship to link multiple objects of one model with multiple objects of another.?
    How to get data for nested fields?
    How to create nested field data using nested serializer?
    How to override create method to handle the nested data represented by the ManyToMany relationship.
    🎧 Music : • Study music | Programm...
    Music Channel : @tuningthecode
    Source Code: github.com/Cod...
    Many To One relationship Video:
    • Django Rest Framework ...
    One To One relationship Video:
    • Django Rest Framework ...

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

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

    thanks a lot!!!!! u help me a lot man thanks thanks !!!!

  • @tomilola_ng
    @tomilola_ng 15 วันที่ผ่านมา +1

    It saved my life bro, well done, Great Job

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

    This approach works, but it also overrides all of the validations provided by the serializer class, which could lead to problems down the road. I used this video as a stepping stone, then moved it to the create() method in the serializer class:
    class StudentsSerializer(serializers.ModelSerializer):
    class Meta:
    model = Students
    fields = ['id', 'name', 'age', 'grade', 'modules']
    depth = 1
    def create(self, validated_data):
    module_data = validated_data.pop('modules', [])
    new_student = Student.objects.create(**validated_data)
    for module in module_data:
    module_obj = Modules.objects.get(module_name=module["module_name"])
    new_student.modules.add(module_obj)
    return new_student

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

      hi, but did you keep the create method in the view too? if i override only the serializer method it doesn't work.. thanks

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

      @@robyguglie no I removed the create method from the view. Try making this mod to ModuleSerializer:
      class ModuleSerializer(serializers.ModelSerializer):
      id = IntegerField(label='ID')
      by default, id is set to read_only=True, so it needs to be overridden or it won't be added to your validated_data in StudentSerializer.

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

      so it seems like you combined the Student viewset and the Student serializer essentially?

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

      also can anyone explain what validations are being overridden and whats so bad about it?

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

      Hi thanks for this, would it be possible to send not a list of objects but just a list of module ids (like [1,2,3]) in the post body.

  • @shwetajha6476
    @shwetajha6476 5 หลายเดือนก่อน +1

    Thank you very much helped alot❤

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

    Thank you so much man ❤️
    This is exactly what I needed.

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

    Waoo, thanks very very much! This is my solutions in apiRest parents child or main and detail! Nice!

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

    Thanks!

  • @think-dotest2166
    @think-dotest2166 3 ปีที่แล้ว +2

    Great, thanks for the well explained video 👍🏻

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

    Great tutorial. Thanks for sharing!

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

    Thanks, really informative👍🏽👍🏽

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

    How add filters for ManyToMany. Like filtering list of students took physics.

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

    Thanks a lot for this video!!!

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

    10x! your example helped me :)

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

    Thank you very mucho. CLCL

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

    Really helpful bro thanks

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

    Excelent material thank you very much! But the audio its kind of bad too much mouth noises

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

      Thank you for the support and feedback, will keep that in mind ✌🏻

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

    Is it something I do not understand, or do you create an object bypassing the serializer, and use it only for parsing in JSON? It seems to be a mistake to save data without validation.

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

      Hello Gosha, it’s a different approach, plus you can do data validation on the serializer level or view level. This tutorial doesn’t tackle data validation though.

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

    manyToMany relation is symetrical. What if you also want to display the list of students in a modul record ?

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

    I was wondering what do we need to do if we wanted to create a new student with a completely new module that doesn't exist.

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

      Hello Viktor, you can create the new module object then create the new student object and assign the module to it inside the student view.

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

    Muito obrigado! :)

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

    Hello; sameone help me please
    views.py", line 23, in create
    module_obj = Modules.objects.get(module_name=module["module_name"])
    TypeError: string indices must be integers

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

    I have a similar requirement. Where I wanted to update many to many field which is blank when we create it. In this case like when we create a Student, modules should blank. Can you please let me know how can I achieve it ?
    I have searched a lot but not found any relevant answer .
    Please help.
    Thanks.

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

      Please email me at:
      Codes.environment@gmail.com
      And I’ll help you with it.

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

    Thanks bro

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

    Thank you .. very Nice.. how about for update Many 2 Many, example for update Students Modules ?

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

      But this way will by pass serializers.is_valid ?

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

      Thank you Nabila, for the update you need to override the update method inside your view. Please see this tutorial, it’ll help you understand the concept:
      th-cam.com/video/r0Y_Qp21liI/w-d-xo.html

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

      In this tutorial we are using the serializer to serializer the data only, you can choose to validate your data in the view as well.

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

    nice video

  • @omegaroyal
    @omegaroyal 6 หลายเดือนก่อน

    how do you add and remove modules to and from students that already exist????

    • @omegaroyal
      @omegaroyal 6 หลายเดือนก่อน

      Oh I see it is in the NEXT tutorial. Watching that one next. Thanks for this.

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

    How we can override for update /PUT method. If some modules are added or deleted from the students..then how to do that?

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

      Please help sir. I even emailed u for this 🙏

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

      Please see this tutorial for UPDATE / PUT methods:
      th-cam.com/video/f2xky4CwQ9Q/w-d-xo.html

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

      @@codeenvironment for many to many fields I m facing problem..how to do for many to many fields?

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

    Love this. BTW can I ask how to implement a custom permission to this?
    For example the app you are trying to do has Author and Book model. Can you teach me how to create custom permission where only the authors of the book can modify the book.

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

      Thanks Jonathan,
      I think the best way to achieve that is by comparing the logged in user with the book's author, and if the result is True then you can perform the changes.

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

    Hello, would the logic be the same for functional views? not class based views

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

      The logic is the same but with the functional views extra work is required

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

    could you please show the post request for create method using form-data?

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

      Do you mean using form-data with post man?

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

      @@codeenvironment yes, nested form-data with file actually.
      {
      "title": "x",
      "details": "y",
      "images": [
      {
      "image": file,
      " caption": caption
      }, { another image}
      ]
      }

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

    Love this video !!
    One question , is there a way to select the modules in code? and not in django admin

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

      Thank you 😊, yes of course, you just need their IDs to get the models then add them.

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

    will it also work if we post the data using form data in postman ? if yes then how we can define its key there?? please tell

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

      You can’t send a nested object using form data

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

      @@codeenvironment Actually I am unable to send this data from Android side , I have posted this data by writing the key multiple times in the postman but Android team is saying we can't write keys multiple times our side then I have tried :-
      ['key1','key2'......] Etc but still not working please help me
      And one more thing is that
      If I directly assign this keys like .add('key1','key2',....)
      Then it is working perfectly

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

      Brother I found it's solution I just iterate over the incoming list and remove the special symbols and finally I made a new list and then saved it 😍😍😍

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

      @@mohitrathore8808 well done 👍🏻

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

      @@codeenvironment thanks bro😋

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

    I have a question, what should be done to view all students linked to a module?

  • @ravindrakumara.
    @ravindrakumara. 3 ปีที่แล้ว

    Many to Many with serializer how to get same method?

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

    Hi, do you think you could expand on that video with custom 'Through' models in Many2many relationships? I have an issue with something i'm trying to build:
    ```
    modifiers = models.ManyToManyField(Modifier, through='ItemModifier', related_name='items')
    ```
    Once put through a serializer, it returns the serialized item for Modifier instead of ItemModifier, which is supposed to have extra fields

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

      Please email me at:
      Codes.environment@gmail.com
      And I’ll help you

  • @ravindrakumara.
    @ravindrakumara. 3 ปีที่แล้ว

    Server Error (500)