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
HariHaraSudhan
  • 1,168
  • 1
  • 15
  • 32
  • 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 '19 at 09:54

2 Answers2

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

Nakul Narayanan
  • 1,082
  • 11
  • 14
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.

Aarif
  • 1,169
  • 1
  • 16
  • 26