Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/seebass/django-rest-hal

HAL Implementation for Django REST Framework
https://github.com/seebass/django-rest-hal

Last synced: 2 months ago
JSON representation

HAL Implementation for Django REST Framework

Awesome Lists containing this project

README

        

django-rest-hal
===============

HAL Implementation for Django REST Framework

Includes:

* HAL Implemenentation without 'curies'
* Defining fields through url-parameter 'fields'
* Disable link generation through url-parameter 'no-links=true'

For Django REST Framework 3 the have a look at these libs:

* https://github.com/seebass/drf-hal-json
* https://github.com/seebass/drf-nested-fields

## Setup ##

Include the following settings in your django settings.py

'DEFAULT_MODEL_SERIALIZER_CLASS': 'django_rest_hal.serializers.HalModelSerializer',
'DEFAULT_PAGINATION_SERIALIZER_CLASS': 'django_rest_hal.serializers.HalPaginationSerializer',
'DEFAULT_PARSER_CLASSES': ('django_rest_hal.parsers.JsonHalParser',),
'DEFAULT_RENDERER_CLASSES': (
'django_rest_hal.renderers.JsonHalRenderer'
)

## Usage ##

Performing REST-Requests results in following HTTP-Responses:

GET http://localhost/api/resources/1/ HTTP/1.1
Content-Type application/hal+json

{
"_links": {
"self": "http://localhost/api/resources/1/",
"relatedResource": "http://localhost/api/related-resources/1/"
},
"id": 1,
"_embedded": {
"subResource": {
"_links": {
"self": "http://localhost/resources/1/sub-resources/26/"
"subSubResource": "http://localhost/resources/1/sub-resources/26/sub-sub-resources/3"
},
"id": 26,
"name": "Sub Resource 26"
}
}
}

Field customization can be declared using the URL-Query-Parameter 'fields':

GET http://localhost/api/resources/1/?fields=id,subResource.fields(name,subSubResource.fields(id) HTTP/1.1
Content-Type application/hal+json

{
"_links": {
"self": "http://localhost/api/resources/1/",
},
"id": 1,
"_embedded": {
"subResource": {
"_links": {
"self": "http://localhost/resources/1/sub-resources/26/"

},
"name": "Sub Resource 26"
"_embedded": {
"subSubResource": {
"_links": {
"self": "http://localhost/resources/1/sub-resources/26/sub-sub-resources/3"
}
"id": 3
}

}
}
}
}