Dynamically select only a subset of fields per DRF resource(Support both flat and nested resources)
Project description
django-restql
django-restql is a python library which allows you to turn your API made with Django REST Framework(DRF) into a GraphQL like API. With this you will be able to
-
Send a query to your API and get exactly what you need, nothing more and nothing less.
-
Control the data you get, not the server.
-
Get predictable results, since you control what you get from the server.
-
Save the load of fetching unused data from the server.
Isn't it cool?.
django-restql works by selecting dynamically a subset of fields per DRF resource as specified by the request's query
parameter.
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 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]
},
...
]
django-restql support querying both flat and nested resources, so you can expand or query nested fields at any level as long as your field is defined as nested field on a serializer. 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"
}
]
},
...
]
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 inheritDynamicFieldsMixin
and change it as shown belowfrom django_restql.mixins import DynamicFieldsMixin class MyDynamicFieldMixin(DynamicFieldsMixin): query_param_name = "your_favourite_name"
Now you can use this Mixin on your view and use the name
your_favourite_name
as your parameter. E.gGET /users/?your_favourite_name=["id", "username"]
-
Customize how it filter fields to include in a response. You can do this by inheriting DynamicFieldsMixin and override
list
andretrieve
methods as shown below.from django_restql.mixins import DynamicFieldsMixin class CustomDynamicFieldMixin(DynamicFieldsMixin): def list(self, request): # Your customization here return response def retrieve(self, request): # Your customization here return response
Note: To be able to do this you must understand how django-restql is implemented, specifically DynamicFieldsMixin class, you can check it here. In fact this is how django-restql is implemented(overriding
list
andretrieve
methods of a view, nothing more and nothing less).
Running Tests
python setup.py test
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
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
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
Built Distribution
File details
Details for the file django-restql-0.2.1.tar.gz
.
File metadata
- Download URL: django-restql-0.2.1.tar.gz
- Upload date:
- Size: 4.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95ba58d35e097d4e20c2d15e8691eebb57e1e83b117921ba274375baf14c2919 |
|
MD5 | 539cfa5a642bbb829e17206bd859a133 |
|
BLAKE2b-256 | abaa478ebb5282df418a7c15056570b751dc95b2ec5be54a1ccf94f2b613a442 |
File details
Details for the file django_restql-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: django_restql-0.2.1-py3-none-any.whl
- Upload date:
- Size: 5.3 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | f979c2d8947e3ab3ec7202b2f7277d9f35d9d34731a648622ff529f438ab2947 |
|
MD5 | f74ee2f31f6beb011a739d36be470353 |
|
BLAKE2b-256 | c1d12b0acc5fe413060c443413fb9525e790e41c8eef39f074da9d1dec95b5c1 |