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.

Files for django-restql, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size django_restql-0.1.0-py3-none-any.whl (4.1 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-restql-0.1.0.tar.gz (3.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page