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 django-rest-framework to dynamically select only a subset of fields per DRF resource(Support both flat and nested resources)
Installing
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 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.
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
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 serializers and use the name
your_favourite_name
as your parameter. E.gGET /users/?your_favourite_name=["id", "username"]
-
Customize how django-restql serialize flat fields, nested flat fields and nested iterable fields.
You can do this by inheriting
DynamicFieldsMixin
and overrideflat_field
,nested_flat_field
andnested_iter_field
methods as shown below.from django_restql.mixins import DynamicFieldsMixin class MyDynamicFieldMixin(DynamicFieldsMixin): def flat_field(self, field_value, parent_field, field_name): # Your customization here return flat_field_value def nested_flat_field(self, field_value, parent_field, field_name): # Your customization here return flat_field_value def nested_iter_field(self, field_value, parent_field, field_name): # Your customization here return flat_field_value
Note: To be able to do this you must understand how django-restql is implemented, specifically DynamicFieldsMixin class, you can check it here.
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](https://pypi-camo.freetls.fastly.net/a8897c5f5e8a5eacb8ddf983bb640b73cc10464c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)
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
Hashes for django_restql-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f490c49af209924537946e5ab2644c4a9a3d51a8bf865987ddcff7c183bcb457 |
|
MD5 | 72c3a7f143876627edb6cb93b6408f44 |
|
BLAKE2b-256 | 43448f0d5df5e0a2c5eafad5f66b5b7c071a0f89292c3db9fbabf38eff3e5ebf |