Skip to main content

Utilities to reduce the boiler-plating of django-rest-framework-json-api

Project description

DRF Json Api Utils

Utilities to massivly reduce the boiler-plating of django-rest-framework.

This project currenly only supports and is specific to django-rest-framework-json-api, if requested will extend it to general django-rest-framework support.

The source for this project is available here.

  • Add better docs.

Example Usage

By just doing this:

user_urls = JsonApiViewBuilder(model=MyUser, 
                               resource_name='users',
                               allowed_methods=[json_api_spec_http_methods.HTTP_GET]) \
.fields(['email', 'first_name', 'last_name']) \
.add_filter(name='email', lookups=(lookups.EXACT, lookups.IN)) \
.add_filter(name='order', field='orders__id', lookups=(lookups.EXACT, lookups.IN)) \
.add_relation(field='orders', many=True) \
.get_urls()

order_urls = JsonApiViewBuilder(model=Order, 
                               resource_name='orders',
                               allowed_methods=[json_api_spec_http_methods.HTTP_GET]) \
.fields(['product', 'date', 'price']) \
.add_relation(field='user', resource_name='users') \
.get_urls()

You can get this:

GET /api/users?filter[email]=amit.assaraf@gmail.com

{
    "links": {
        "first": "http://localhost:8000/api/users?filter%5Bemail%5D=soit48%40gmail.com&page%5Bnumber%5D=1",
        "last": "http://localhost:8000/api/users?filter%5Bemail%5D=soit48%40gmail.com&page%5Bnumber%5D=1",
        "next": null,
        "prev": null
    },
    "data": [
        {
            "type": "users",
            "id": "76f7c463-b6a8-4b20-917a-ef98c546eec4",
            "attributes": {
                "first-name": "Amit",
                "last-name": "Assaraf",
                "email": "amit.assaraf@gmail.com",
            },
            "relationships": {
                "orders": {
                    "meta": {
                        "count": 2
                    },
                    "data": [
                        {
                            "type": "orders",
                            "id": "6b99e044-462f-472e-9fed-307436b73549"
                        },
                        {
                            "type": "orders",
                            "id": "304f7f60-7e24-494e-98e2-e9786a3eb588"
                        }
                    ],
                    "links": {
                        "self": "http://localhost:8000/api/users/76f7c463-b6a8-4b20-917a-ef98c546eec4/relationships/orders",
                        "related": "http://localhost:8000/api/orders/76f7c463-b6a8-4b20-917a-ef98c546eec4/"
                    }
                }
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pages": 1,
            "count": 1
        }
    }
}

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

drf-json-api-utils-2.0.78.tar.gz (29.2 kB view details)

Uploaded Source

File details

Details for the file drf-json-api-utils-2.0.78.tar.gz.

File metadata

  • Download URL: drf-json-api-utils-2.0.78.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for drf-json-api-utils-2.0.78.tar.gz
Algorithm Hash digest
SHA256 79e6f4d4395088846be19ac611805721c0dcb5cf8320c25ab41384f45bcad531
MD5 5fa7d5cfc467cb1d0c9b449e7ebc3fbd
BLAKE2b-256 e73e63c6aec455be666f2d3c514cf82ec12cd1070e60915bb59713256b721e1c

See more details on using hashes here.

Supported by

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