Skip to main content

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.

Source Distribution

File details

Details for the file django-rest-framework-google-json-style-api-1.0.1.tar.gz.

File metadata

  • Download URL: django-rest-framework-google-json-style-api-1.0.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.16

File hashes

Hashes for django-rest-framework-google-json-style-api-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0a6a0793091959ae741fd73f53a5887fe1beb0b6199d4496f852c86e059d2386
MD5 a9ae030e44c0efd8dc18ff497b12c2fe
BLAKE2b-256 b8ed7892187065b264d2a4a3c53c686eec979c66861ebafe74fb82ad81448546

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page