Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

Collection+JSON support for Django REST Framework

Project Description

This library adds support for the Collection+JSON hypermedia format to Django Rest Framework. For more information on Collection+JSON see the official Collection+JSON documentation.

Unit Testing

To test the package run the following command:

python setup.py test

Usage

To enable the Collection+JSON renderer, either add it as a default renderer in your django settings file:

'DEFAULT_RENDERER_CLASSES': (
    'rest_framework_cj.renderers.CollectionJsonRenderer',
)

or explicitly set the renderer on your view:

class MyViewSet(ReadOnlyModelViewSet):
    renderer_classes = (CollectionJsonRenderer, )

Renderer Behavior

The Collection+JSON renderer will do it’s best to support the built in Django Rest Framework Serializers and Views. However, the renderer is designed to work with Django Rest Framework’s hyperlinked views/serializers.

Given a simple model and an associated view/serializer:

class Dummy(Model):
    name = CharField(max_length='100')

class DummyHyperlinkedModelSerializer(HyperlinkedModelSerializer):
    class Meta(object):
        model = Dummy
        fields = ('url', 'name', )

class DummyReadOnlyModelViewSet(ReadOnlyModelViewSet):
    renderer_classes = (CollectionJsonRenderer, )
    queryset = Dummy.objects.all()
    serializer_class = DummyHyperlinkedModelSerializer

If you register the view as follows:

router = DefaultRouter()
router.register('dummy', DummyReadOnlyModelViewSet)
urlpatterns = patterns(
    '',
    (r'^rest-api/', include(router.urls)),
)

Navigating to the url /rest-api/dummy/ will generate a collection+JSON containing serialized dummy objects in it’s items array.:

"items": [
    {
        "href": "http://foobar.com/rest-api/dummy/1"/,
        "data": [
            {
                "name": "name",
                "value": "foo"
            },
        ]
    },
    {
        "href": "http://foobar.com/rest-api/dummy/2/",
        "data": [
            {
                "name": "name",
                "value": "bar"
            },
        ]
    }
]

Foreign key/Many to Many relationships will be rendered in an item’s links array:

children = ManyToManyField('Child')

"links": [
    {
        "href": "http://foobar.com/rest-api/child/1/",
        "rel": "children"
    },
    {
        "href": "http://foobar.com/rest-api/child/2/",
        "rel": "children"
    },
]

The renderer will also recognize the default router and provide links its resources:

{
    "collection": {
        "href": "http://foobar.com/rest-api/",
        "items": [],
        "version": "1.0",
        "links": [
            {
                "href": "http://foobar.com/rest-api/dummy/",
                "rel": "dummy"
            },
        ]
    }
}
Release History

Release History

This version
History Node

0.0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
djangorestframework_collection_json-0.0.1-py2.py3-none-any.whl (26.3 kB) Copy SHA256 Checksum SHA256 2.7 Wheel Aug 19, 2014
djangorestframework-collection-json-0.0.1.tar.gz (9.4 kB) Copy SHA256 Checksum SHA256 Source Aug 19, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting