Skip to main content

Dynamically select only a subset of fields per DRF resource(Support both flat and nested resources)

Project description

django-restql

Latest Version Python Versions License

django-restql is a python library which allows django-rest-framework to dynamically select only a subset of fields per DRF resource(Support both flat and nested resources)

Installing

For python3

pip3 install dictfier

For python2

pip install dictfier

Getting Started

Using django-restql is very simple, you just have to use the DynamicFieldsMixin when defining a serializer.

from rest_framework import serializers
from django.contrib.auth.models import User
from django_restql.mixins import DynamicFieldsMixin

class UserSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'groups')

A regular request returns all fields:

GET /users

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "email": "yezileliilomo@hotmail.com",
        "groups": [1,2]
      },
      ...
    ]

A request with the query parameter on the other hand returns only a subset of the fields:

GET /users/?query=["id", "username"]

    [
      {
        "id": 1,
        "username": "yezyilomo"
      },
      ...
    ]

With django-restql you can access nested fields of any level. E.g

GET /users/?query=["id", "username" {"date_joined": ["year"]}]

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "date_joined": {
            "year": 2018
        }
      },
      ...
    ]

django-restql got your back on iterable nested fields too. E.g

GET /users/?query=["id", "username" {"groups": [[ "id", "name" ]]}]

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "groups": [
            {
                "id": 2,
                "name": "Auth_User"
            }
        ]
      },
      ...
    ]

Warnings If the request context does not have access to the request, a warning is emitted:

UserWarning: Context does not have access to request.

First, make sure that you are passing the request to the serializer context

Credits

This implementation is based on dictfier library and the idea behind GraphQL.

My intention was to extend the capability of drf-dynamic-fields library to support more functionalities like allowing to query nested fields both flat and iterable.

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

django-restql-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

django_restql-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file django-restql-0.1.0.tar.gz.

File metadata

  • Download URL: django-restql-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.11.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for django-restql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ad242bf26c4310c4510e90da3989b12ee492c4b848a8d88782ce5e301626df03
MD5 1cdc2d6ea0648003e68fa8ff5e0c9908
BLAKE2b-256 87111d9db09669d95f33d0fb08d39b515abb5afed24078448f570d4b592df803

See more details on using hashes here.

File details

Details for the file django_restql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_restql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.11.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for django_restql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5a3c7b2169c9272def3f4a10d323f289fca115f7e16bab7a2fe726dbb07d057
MD5 a1a7d3bf497e826caac2502fbc0d54f9
BLAKE2b-256 0bb8a12ab01b115bef2167398bd91781f43439b9bd09c3fffe71b1618378da2c

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