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

pip install django-restql

Getting Started

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

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

class UserViewSet(DynamicFieldsMixin, viewsets.ModelViewSet):
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer

A regular request returns all fields specified on DRF serializer, in fact django-restql doesn't handle this request at all:

GET /users

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

django-restql handle all GET requests with query parameter, this parameter is the one used to pass all fields to be included on a response. For example to select id and username fields from user model, send a request with a query parameter as shown below.

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

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

If a query contains nested field, django-restql will return its id or array of ids for the case of nested iterable field(one2many or many2many). For example on a request below location is a flat nested field(many2one) and groups is an iterable nested field(one2many or many2many).

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

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "location": 6,
        "groups": [1,2]
      },
      ...
    ]

With django-restql you can expand or query nested fields at any level. For example you can query a country and region field from location.

GET /users/?query=[["id", "username", {"location": ["country", "region"]}]]

    [
      {
        "id": 1,
        "username": "yezyilomo",
        "location": {
            "contry": "Tanzania",
            "region": "Dar es salaam"
        }
      },
      ...
    ]

django-restql got your back on expanding or querying iterable nested fields too. For example if you want to expand groups field into id and name, here is how you would do it.

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

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

Note:

The notation used to expand flat nested fields is field_name=[sub_field1, sub_field2, ...]

And for iterable nested fields field_name=[[sub_field1, sub_field2, ...]]

For more information on how to create queries you can refer to dictfier which is a library used to implement this project.

Customizing django-restql

django-restql is very configurable, here is what you can customize

  • Change the name of query parameter.

    If you don't want to use the name query as your parameter, you can inherit DynamicFieldsMixin and change it as shown below

    from django_restql.mixins import DynamicFieldsMixin
    
    class MyDynamicFieldMixin(DynamicFieldsMixin):
        query_param_name = "your_favourite_name"
    

    Now you can use this Mixin on your serializers and use the name your_favourite_name as your parameter. E.g

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

Credits

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

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

Contributing PRs Welcome

We welcome all contributions. Please read our CONTRIBUTING.md first. You can submit any ideas as pull requests or as GitHub issues. If you'd like to improve code, check out the Code Style Guide and have a good time!.

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.2.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

django_restql-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-restql-0.2.0.tar.gz
  • Upload date:
  • Size: 3.9 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.2.0.tar.gz
Algorithm Hash digest
SHA256 ac4c276f528a2369e60cc33de2c5289d6524f336b1d66fe5ed7560118fe3d10c
MD5 3674f5a0c6da66d95ced82d1f6ce88ed
BLAKE2b-256 cf9dd79eaf21c1126231a09ad85f15e40c4815ecb4d2a45c54cc385ad7b92760

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_restql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e6debb314ad5597ea35eb817d083397aa1e06ad233bd948cc44f21d8d43164d
MD5 f14d13173ea7a980aa418fae84bd43d8
BLAKE2b-256 af66e131f898c48bec273d97f36aa488923b5f00a97cefa053ab248a9531c829

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