Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-Rest-Framework, update foreign key by ID when using HyperlinkedModelSerializer

This question is derived based on Django-Rest-Framework updating a foreign key BY Id.

I have a somewhat similar problem. I love HyperlinkedModelSerializer. I could navigate through all the links of the API from my web browser (e.g. Chrome, FF, etc.) but when I try to use the API, I have a much work to do in the client app. I have no issue with the GET request. In POST request when updating a model with ForeignKey, I need to construct the URL from the client app (e.g. AngularJS, Android, etc.) before making the POST request to the server. I'm aware of the ModelSerializer which solve the problem from the client app, but it is not navigable from the web browser.

I'm not sure what is a good approach in designing browsable REST API. I'm not sure how most people solve this problem, when they want to create a browsable REST API, at the same time, they don't want to add the complexity on the client app by having to parse the URL-ID before making POST request. Or could this be just my problem that no body encounter.

Why not HyperlinkedModelSerializer does the following instead.

  • return all the ForeignKey in URL upon GET request. So that developer could navigate the API from their web browser.
  • accepting ID upon POST request. So that developer could just pass the ID rather than having to construct the full URL from the client app.

Example:

c = Client()

data = {
    'user': '1',
    'industry': '1'
}

c.post('http://localhost:8000/favorite_industries/', json.dumps(data), 'application/json')

response = c.get('http://localhost:8000/favorite_industries/')
print(response.content)
# [{"id": 1, "user": "http://localhost:8000/users/1/", "industry": "http://localhost:8000/industries/1/"}]

Question:

  • What could be the advantage from the current design of HyperlinkedModelSerializer?

  • What could be the drawback from my suggestion?

  • How can it be done?

like image 212
Yeo Avatar asked Nov 10 '22 12:11

Yeo


1 Answers

I don't see a need to construct URLs at all. When you are sending foreign keys, you are basically referencing another object. This other object you should already know its identifier. In your example, the user id is 1. If you build your API around HyperlinkedModelSerializer, user object will come with its own identifier: url. Before creating your favorite_industries object, you need to know which user to associate with. In normal situations you will have the user object including its url:

{
   "url" : "http://localhost:8000/users/1",
   "name": "Yeo"
}

All you need to do is sending this identifier as a foreign key:

data = {
    'user': 'http://localhost:8000/users/1',
    'industry': 'http://localhost:8000/industries/1'
}

I say in normal situations because usually in your client app ids are not entered by users but other info like name are displayed for the user to pick which mandates having the full user object including its url.

like image 66
almalki Avatar answered Nov 15 '22 07:11

almalki