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.2.3.tar.gz.

File metadata

  • Download URL: django-rest-framework-google-json-style-api-1.2.3.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.7

File hashes

Hashes for django-rest-framework-google-json-style-api-1.2.3.tar.gz
Algorithm Hash digest
SHA256 df560a4faf14f4d129d126cfbc3ef7c29c1cae8b7e95596439a1f827a6ac3dd0
MD5 18d05c53393162e3987383ac510582a5
BLAKE2b-256 538a2624564f760b765f001ed10e7470edc40f6263f9c74196e0ccb618db9e9e

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