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

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 70

  • @mastermaster153
    @mastermaster153 2 роки тому +3

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

  • @tomilola_ng
    @tomilola_ng 3 місяці тому +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.

  • @aaronjaffetgutierrez1345
    @aaronjaffetgutierrez1345 2 роки тому +1

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

  • @aryanshrestha4846
    @aryanshrestha4846 3 роки тому +2

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

  • @think-dotest2166
    @think-dotest2166 4 роки тому +2

    Great, thanks for the well explained video 👍🏻

  • @MegaMooj
    @MegaMooj 3 роки тому +1

    Great tutorial. Thanks for sharing!

  • @aashayamballi
    @aashayamballi 4 роки тому +2

    Thanks, really informative👍🏽👍🏽

  • @shwetajha6476
    @shwetajha6476 9 місяців тому +1

    Thank you very much helped alot❤

  • @eranlondon
    @eranlondon 2 роки тому +1

    10x! your example helped me :)

  • @josiahdavid7735
    @josiahdavid7735 3 роки тому +1

    Thanks a lot for this video!!!

  • @deepakbiradar4060
    @deepakbiradar4060 4 роки тому +1

    Really helpful bro thanks

  • @NavaneethaKrishnan007
    @NavaneethaKrishnan007 3 роки тому +1

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

  • @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 ✌🏻

  • @GustavoPMex
    @GustavoPMex 2 роки тому +1

    Thanks!

  • @SieteGL
    @SieteGL 3 роки тому +1

    Thank you very mucho. CLCL

  • @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:
      ua-cam.com/video/r0Y_Qp21liI/v-deo.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.

  • @felipebrx7761
    @felipebrx7761 3 роки тому +1

    Muito obrigado! :)

  • @surenlama5265
    @surenlama5265 3 роки тому +1

    nice video

  • @charlesenglebert8226
    @charlesenglebert8226 Рік тому

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

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

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

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

  • @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:
      ua-cam.com/video/f2xky4CwQ9Q/v-deo.html

    • @mansiarora3069
      @mansiarora3069 3 роки тому

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

  • @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}
      ]
      }

  • @TheZayzoo
    @TheZayzoo 3 роки тому +1

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

    • @codeenvironment
      @codeenvironment  3 роки тому +1

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

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

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

  • @AbdexKeni
    @AbdexKeni 3 роки тому +1

    Thanks bro

  • @MsGoshaaaa
    @MsGoshaaaa 3 роки тому +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  3 роки тому

      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.

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

  • @omegaroyal
    @omegaroyal 9 місяців тому

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

    • @omegaroyal
      @omegaroyal 9 місяців тому

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

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