How to save Foreign Key values in DJANGO REST?What is a “slug” in Django?How to write a Custom field Validation for ModelSerializers in Django Rest Framework(DRF) similar to Form Validation in Django?save() prohibited to prevent data loss due to unsaved related object 'postid'Posting a foreign key relationship in Django Rest FrameworkSerializing custom related field in DRFDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerdjango rest framework and forms: How to doDjango Model Issue In Multilevel Inheritance.How to implement update_or_create inside create method of ModelSerializerf string interpolation syntax error python3.6How to check if Django Signal works?

Would it be a copyright violation if I made a character’s full name refer to a song?

How are the Zhentarim and Black Fist related?

In the Marvel universe, can a human have a baby with any non-human?

What does "play with your toy’s toys" mean?

Why do some professors with PhDs leave their professorships to teach high school?

How was Hillel permitted to go to the skylight to hear the shiur

How to make clear to people I don't want to answer their "Where are you from?" question?

Accidentals and ties

Are all instances of trolls turning to stone ultimately references back to Tolkien?

How many people are necessary to maintain modern civilisation?

Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback

Find the diameter of a word graph

Links to webpages in books

How is hair tissue mineral analysis performed?

Should developer taking test phones home or put in office?

What's currently blocking the construction of the wall between Mexico and the US?

Is there a maximum distance from a planet that a moon can orbit?

Can ADFS connect to other SSO services?

Can Ogre clerics use Purify Food and Drink on humanoid characters?

C-152 carb heat on before landing in hot weather?

Why do some games show lights shine thorugh walls?

What was the Shuttle Carrier Aircraft escape tunnel?

Is using weak login credentials always bad?

Folding basket - is there such a thing?



How to save Foreign Key values in DJANGO REST?


What is a “slug” in Django?How to write a Custom field Validation for ModelSerializers in Django Rest Framework(DRF) similar to Form Validation in Django?save() prohibited to prevent data loss due to unsaved related object 'postid'Posting a foreign key relationship in Django Rest FrameworkSerializing custom related field in DRFDjango-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerdjango rest framework and forms: How to doDjango Model Issue In Multilevel Inheritance.How to implement update_or_create inside create method of ModelSerializerf string interpolation syntax error python3.6How to check if Django Signal works?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Let's say i have models like follow.



class Department(models.Model):
name = models.CharField(max_length=255)

class Student(models.Model):
department = models.Foreignkey(Department, on_delete=models.CASCADE)
name = models.CharField(max_length=255)


Now i need to set Department(which is created already) of the Student whenever i create the student instance.



which is best practice



i) Sending the department id in URI like `departments/pk/students`
ii) sending department id in body parameter









share|improve this question






















  • department id in the body and the endpoint should look like /student/ (not adding department in URL and keeping it simple) with an HTTP POST

    – Aarif
    Mar 25 at 9:54

















0















Let's say i have models like follow.



class Department(models.Model):
name = models.CharField(max_length=255)

class Student(models.Model):
department = models.Foreignkey(Department, on_delete=models.CASCADE)
name = models.CharField(max_length=255)


Now i need to set Department(which is created already) of the Student whenever i create the student instance.



which is best practice



i) Sending the department id in URI like `departments/pk/students`
ii) sending department id in body parameter









share|improve this question






















  • department id in the body and the endpoint should look like /student/ (not adding department in URL and keeping it simple) with an HTTP POST

    – Aarif
    Mar 25 at 9:54













0












0








0








Let's say i have models like follow.



class Department(models.Model):
name = models.CharField(max_length=255)

class Student(models.Model):
department = models.Foreignkey(Department, on_delete=models.CASCADE)
name = models.CharField(max_length=255)


Now i need to set Department(which is created already) of the Student whenever i create the student instance.



which is best practice



i) Sending the department id in URI like `departments/pk/students`
ii) sending department id in body parameter









share|improve this question














Let's say i have models like follow.



class Department(models.Model):
name = models.CharField(max_length=255)

class Student(models.Model):
department = models.Foreignkey(Department, on_delete=models.CASCADE)
name = models.CharField(max_length=255)


Now i need to set Department(which is created already) of the Student whenever i create the student instance.



which is best practice



i) Sending the department id in URI like `departments/pk/students`
ii) sending department id in body parameter






django-rest-framework python-3.6 django-serializer django-2.1






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 9:34









HariHaraSudhanHariHaraSudhan

5211 gold badge6 silver badges23 bronze badges




5211 gold badge6 silver badges23 bronze badges












  • department id in the body and the endpoint should look like /student/ (not adding department in URL and keeping it simple) with an HTTP POST

    – Aarif
    Mar 25 at 9:54

















  • department id in the body and the endpoint should look like /student/ (not adding department in URL and keeping it simple) with an HTTP POST

    – Aarif
    Mar 25 at 9:54
















department id in the body and the endpoint should look like /student/ (not adding department in URL and keeping it simple) with an HTTP POST

– Aarif
Mar 25 at 9:54





department id in the body and the endpoint should look like /student/ (not adding department in URL and keeping it simple) with an HTTP POST

– Aarif
Mar 25 at 9:54












2 Answers
2






active

oldest

votes


















1














It is always better to send the sensitive data like ids as Post request instead of passing as URL args. if you really want to pass data in URL then please use slugs instead of ID.



also, you can use DRF model serializer to save the data






share|improve this answer






























    1














    in this case, It'd be better to send department id in the body and keep the endpoint simple as



    base/api/student/


    make an HTTP POST to this endpoint




    "department_id":"",
    .
    .



    by taking a look at this endpoint this clearly shows endpoint for operations involving student object, I'd say it's more close to REST standards.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55334807%2fhow-to-save-foreign-key-values-in-django-rest%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      It is always better to send the sensitive data like ids as Post request instead of passing as URL args. if you really want to pass data in URL then please use slugs instead of ID.



      also, you can use DRF model serializer to save the data






      share|improve this answer



























        1














        It is always better to send the sensitive data like ids as Post request instead of passing as URL args. if you really want to pass data in URL then please use slugs instead of ID.



        also, you can use DRF model serializer to save the data






        share|improve this answer

























          1












          1








          1







          It is always better to send the sensitive data like ids as Post request instead of passing as URL args. if you really want to pass data in URL then please use slugs instead of ID.



          also, you can use DRF model serializer to save the data






          share|improve this answer













          It is always better to send the sensitive data like ids as Post request instead of passing as URL args. if you really want to pass data in URL then please use slugs instead of ID.



          also, you can use DRF model serializer to save the data







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 9:42









          Nakul NarayananNakul Narayanan

          6177 silver badges11 bronze badges




          6177 silver badges11 bronze badges























              1














              in this case, It'd be better to send department id in the body and keep the endpoint simple as



              base/api/student/


              make an HTTP POST to this endpoint




              "department_id":"",
              .
              .



              by taking a look at this endpoint this clearly shows endpoint for operations involving student object, I'd say it's more close to REST standards.






              share|improve this answer



























                1














                in this case, It'd be better to send department id in the body and keep the endpoint simple as



                base/api/student/


                make an HTTP POST to this endpoint




                "department_id":"",
                .
                .



                by taking a look at this endpoint this clearly shows endpoint for operations involving student object, I'd say it's more close to REST standards.






                share|improve this answer

























                  1












                  1








                  1







                  in this case, It'd be better to send department id in the body and keep the endpoint simple as



                  base/api/student/


                  make an HTTP POST to this endpoint




                  "department_id":"",
                  .
                  .



                  by taking a look at this endpoint this clearly shows endpoint for operations involving student object, I'd say it's more close to REST standards.






                  share|improve this answer













                  in this case, It'd be better to send department id in the body and keep the endpoint simple as



                  base/api/student/


                  make an HTTP POST to this endpoint




                  "department_id":"",
                  .
                  .



                  by taking a look at this endpoint this clearly shows endpoint for operations involving student object, I'd say it's more close to REST standards.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 25 at 10:03









                  AarifAarif

                  6127 silver badges18 bronze badges




                  6127 silver badges18 bronze badges



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55334807%2fhow-to-save-foreign-key-values-in-django-rest%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript