Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Make Google Json Style and Django Rest Framework play nice together.

Project description

Django REST Framework Google JSON Style API

Build Status codecov.io PyPI version

Format specification

Installation

At the command line:

$ pip install django-rest-framework-google-json-style-api

Add the render and parser to your django settings file.

# ...
REST_FRAMEWORK = {
    'PAGE_SIZE': 10,
    'DEFAULT_PAGINATION_CLASS': 
        'rest_framework_google_json_style_api.pagination.GoogleJsonStylePageNumberPagination',
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework_google_json_style_api.renderers.JSONRenderer',
        # Any other renders
    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework_google_json_style_api.parsers.JSONParser',
        # Any other parsers
    ),
}
# ...

Goals

By default, Django REST Framework will produce a response like:

{
    "id": 1,
    "username": "scott",
    "full_name": "Scott Chang"
}

and

[
    {
        "id": 1,
        "username": "scott",
        "full_name": "Scott Chang"
    },
    {
        "id": 2,
        "username": "pocheng",
        "full_name": "Pocheng Huang"
    }
]

Google JSON Style Guide shows a response to look like the following:

{
    "data": {
        "id": 1,
        "username": "scott",
        "fullName": "Scott Chang"
    }
}

and

{
    "data": {
        "items":[
            {
                "id": 1,
                "username": "scott",
                "fullName": "Scott Chang"
            },
            {
                "id": 2,
                "username": "pocheng",
                "fullName": "Pocheng Huang"
            }
        ]
    }
}

Google JSON Style Guide uses camel case for field names. So, by default the package uses camel case. If you want to use underscores, you must specify it in your django settings file.

GOOGLE_JSON_STYLE_API = {
    # ...
    'CAMELIZE': False
    # ...
}

Pagination

{
    "method": "list",
    "params": {
        "page": "1",
        "pageSize": "2"
    },
    "data": {
        "currentItemCount": 2,
        "itemsPerPage": 2,
        "totalItems": 200,
        "pageIndex": 1,
        "totalPages": 100,
        "nextLink": "http://example.com/api/v1/?page=2&page_size=2",
        "previousLink": null,
        "items": [
            {
                "id": 1,
                "username": "scott",
                "fullName": "Scott Chang"
            },
            {
                "id": 2,
                "username": "pocheng",
                "fullName": "Pocheng Huang"
            }
        ]
    }
}

Attach meta to data object

Example

Let's add the count of items in the data object.

class AuthorViewSet(viewsets.ModelViewSet):
    queryset = Author.objects.all()
    # ...

    def list(self, request, *args, **kwargs):
        response = super(AuthorViewSet, self).list(request, *args, **kwargs)
        response.data = {
            'meta': {
                # Add meta data in here
                'num_items': self.queryset.count(),
            },
            # Keep original data in results
            'results': response.data
        }

Response

{
    "data": {
        "numItems": 2,
        "items":[
            {
                "id": 1,
                "username": "scott",
                "fullName": "Scott Chang"
            },
            {
                "id": 2,
                "username": "pocheng",
                "fullName": "Pocheng Huang"
            }
        ]
    }
}

Underscoreize Options

There are two conventions of snake case.

Case 1 (default)

v2Counter -> v_2_counter
fooBar2 -> foo_bar_2

Case 2

v2Counter -> v2_counter
fooBar2 -> foo_bar2

By default, the package uses the first case. To use the second case, specify it in your django settings file. The setting only works when you use camel case(default).

GOOGLE_JSON_STYLE_API = {
    # ...
    'JSON_UNDERSCOREIZE': {
        'no_underscore_before_number': True,
    },
    # ...
}

Project details


Download files

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

Files for django-rest-framework-google-json-style-api, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size django-rest-framework-google-json-style-api-1.0.0.tar.gz (11.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page