What is best practice to serialize foreign key fields in a REST-ful api

Norman8054

I want to write a Django app with a REST-ful api. Django REST framework offers three built-in model serializers: ModelSerializer which serializes to something like this

{
    'normal_field': 'value',
    'foreign_key_field': 42
}

and HyperlinkedModelSerializer which serializes to something like this

{
    'normal_field': 'value',
    'foreign_key_field': 'http://domain/api/myothermodel/11'
}

My question is whether there is another good way to serialize the data so that the client directly knows which fields have to be resolved and which fields not.

Example: A client which receives this

{ 
    'foo': 'http://domain/api/myothermodel/11',
    'bar': 'http://otherdomain/api/myothermodel/12'
}

does not know whether foo or bar should be a resolveable foreign key field and not a plain url. Something like:

{ 
    'foo': 'http://domain/api/myothermodel/11', # Here the client might know that this is only a plain url.
    'bar': {
            '_foreignkey': true,  # Tells the client that this field should behave as a foreign key which has to be resolved first
            'url': 'http://otherdomain/api/myothermodel/12'
        }
}

Is there any standard or best practice? Or is best practice that the client does not know this from the JSON but from other code it has or gets from the server?

Update: Optional you can add which way is best practice for some wellknown client side libs like AngularJS.

Fiver

It appears you are after a self-describing and explorable web API service, which is actually a core feature of the original REST concept referred to as Hypermedia as the Engine of Application State (HATEOAS). Essentially, a HATEOAS-compliant web service does not rely upon any prior knowledge of the system's resources other than the initial URL entry point.

This is in contrast to a Service Oriented Architecture (SOA) where individual services are basically unassociated and require prior knowledge of their existence to string together several service calls to accomplish some task with multiple, possibly related resources.

Most references to a REST API are actually not fully RESTful, and should more accurately be described as web APIs, as they are more in-line with the SOA design: a set of URLs to both specific instances and collections of instances of a particular type. This would also include the use of different HTTP verbs (GET, PATCH, POST, PUT) for various actions to perform on those resources. The Django REST Framework (DRF) actually has a page on this topic with links to some great resources on the true RESTful design, including the dissertation by Roy Fielding:

http://www.django-rest-framework.org/topics/rest-hypermedia-hateoas

I would speculate that the current use of the term "REST API" relates to the revelation of many developers that implementing a truly RESTful service is far from trivial and in many cases would be over-engineered for the particular use case. Perhaps this is a good example of "perfection as the enemy of the good." For the case of using AngularJS as a client-side framework for interacting with a Django backend via DRF, this is especially true. There is nothing in the AngularJS framework that parses a truly RESTful design to automagically provide the various application states possible for a given resource. Plus, the same developer(s) will usually be responsible for both the JavaScript and Python code, so the lack of a self-describing API is not a major barrier to development.

Regarding the implementation of a truly RESTful API in JSON, there are a couple projects attempting to facilitate this, namely the Hypertext Application Language (HAL) and JSON Linked Data (JSON-LD). However, I am not aware that DRF natively supports either of these, so you would likely have to architect your serializers to conform to one of them, or roll your own Django REST implementation.

Finally, whatever design you choose, fully documenting the API is usually a good idea. This is true regardless of whether the API is web-based or in some native programming language. Part of the allure for the separation of concerns that a web API provides is that 3rd parties can consume resources to build applications or pipelines that you haven't considered, not to mention the advantages in maintainability for future changes to the code base for your project. There are a couple interesting projects mentioned on the DRF site to assist in documenting an API. Swagger is a particularly awesome one, developed by the same developer that provided the older Django REST Framework Docs package.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the best practice for multiple REST API calls with the same parameters

From Dev

What is the best practice to get related/nested data from REST API?

From Dev

What is the best approach for asserting the json fields in rest api automation?

From Dev

Foreign key constraint in REST API

From Dev

What is a best practice for OCaml variants with many fields?

From Dev

Django rest-framework howto serialize a object/foreign key

From Dev

How to serialize tthe foreign key field in django rest framework

From Dev

What is the best practice for using id as primary key?

From Dev

To do foreign key or not to do OR Best practice in building relation through models

From Dev

Best Practice for designing a REST API for subsets of a resource

From Dev

Best Practice for accessing REST API with plaintext password

From Dev

REST API - Best practice for 'real time' data

From Dev

REST API best practice on handling errors

From Dev

Best practice C# REST API

From Dev

What is the best way to serialize contenteditable fields into a JSON object for saving to a model?

From Dev

Best practice for handling api key in Play framework

From Dev

What is the best practice for calling a REST API when a component is shown for the first time?

From Dev

Serialize foreign key's foreign key (DRF)

From Dev

Best Practice for querying data from foreign table in Rails API for React

From Dev

What's the best practice to call API with react

From Dev

Serialize foreign key object that is AbstractUser

From Dev

Serialize model with foreign key Django

From Dev

What is the best practice to protect some fields in a entity for being edited?

From Dev

What is the accepted practice for URL login in API REST?

From Dev

What is the best way to find the max record of a table per a foreign key?

From Dev

Refactor foreign key to fields

From Dev

What's the best practice for error handling with Catalyst::Controller::REST

From Dev

What is the best practice for referencing JSON Schema for your state representations in REST?

From Dev

python django rest framework. How to serialize foreign key UUID in some specific format?

Related Related

  1. 1

    What is the best practice for multiple REST API calls with the same parameters

  2. 2

    What is the best practice to get related/nested data from REST API?

  3. 3

    What is the best approach for asserting the json fields in rest api automation?

  4. 4

    Foreign key constraint in REST API

  5. 5

    What is a best practice for OCaml variants with many fields?

  6. 6

    Django rest-framework howto serialize a object/foreign key

  7. 7

    How to serialize tthe foreign key field in django rest framework

  8. 8

    What is the best practice for using id as primary key?

  9. 9

    To do foreign key or not to do OR Best practice in building relation through models

  10. 10

    Best Practice for designing a REST API for subsets of a resource

  11. 11

    Best Practice for accessing REST API with plaintext password

  12. 12

    REST API - Best practice for 'real time' data

  13. 13

    REST API best practice on handling errors

  14. 14

    Best practice C# REST API

  15. 15

    What is the best way to serialize contenteditable fields into a JSON object for saving to a model?

  16. 16

    Best practice for handling api key in Play framework

  17. 17

    What is the best practice for calling a REST API when a component is shown for the first time?

  18. 18

    Serialize foreign key's foreign key (DRF)

  19. 19

    Best Practice for querying data from foreign table in Rails API for React

  20. 20

    What's the best practice to call API with react

  21. 21

    Serialize foreign key object that is AbstractUser

  22. 22

    Serialize model with foreign key Django

  23. 23

    What is the best practice to protect some fields in a entity for being edited?

  24. 24

    What is the accepted practice for URL login in API REST?

  25. 25

    What is the best way to find the max record of a table per a foreign key?

  26. 26

    Refactor foreign key to fields

  27. 27

    What's the best practice for error handling with Catalyst::Controller::REST

  28. 28

    What is the best practice for referencing JSON Schema for your state representations in REST?

  29. 29

    python django rest framework. How to serialize foreign key UUID in some specific format?

HotTag

Archive