Skip to content

Extension for Django REST Framework 3 which allows for using content-type application/hal-json

License

Notifications You must be signed in to change notification settings

seebass/drf-hal-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

drf-hal-json

Extension for Django REST Framework 3 which allows for using content-type application/hal-json. Its build on top of https://github.com/seebass/drf-nested-fields

Setup

pip install drf-hal-json

REST_FRAMEWORK = {
	'DEFAULT_PAGINATION_CLASS': 'drf_hal_json.pagination.HalPageNumberPagination',
	'DEFAULT_PARSER_CLASSES': ('drf_hal_json.parsers.JsonHalParser',),
	'DEFAULT_RENDERER_CLASSES': ('drf_hal_json.renderers.JsonHalRenderer',),
}

Requirement

  • Python 2.7+
  • Django 1.6+
  • Django REST Framework 3
  • drf-nested-fields 0.9+

Features

By using the HalModelSerializer the content is serialized in the HAL JSON format.

Example

Serializer:

class ResourceSerializer(HalModelSerializer):
	class Meta:
		model = Resource

View:

class ResourceViewSet(HalCreateModelMixin, ModelViewSet):
	serializer_class = ResourceSerializer
	queryset = Resource.objects.all()

Request:

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"
    	}
	}
}

About

Extension for Django REST Framework 3 which allows for using content-type application/hal-json

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages