This package provides a mixin that allows the user to dynamically select only a subset of fields per resource.
Official version support:
Django 2.2 LTS, 3.2 LTS, 4.0
Supported REST Framework versions: 3.8, 3.9
Python 3.7+
Scope
This library is about filtering fields based on individual requests. It is deliberately kept simple and we do not plan to add new features (including support for nested fields). Feel free to contribute improvements, code simplifications and bugfixes though! (See also: #18)
If you need more advanced filtering features, maybe drf-flex-fields could be something for you.
Installing
pip install drf-dynamic-fields
What It Does
Example serializer:
class IdentitySerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Identity
fields = ('id', 'url', 'type', 'data')
A regular request returns all fields:
GET /identities
[
{
"id": 1,
"url": "http://localhost:8000/api/identities/1/",
"type": 5,
"data": "John Doe"
},
...
]
A query with the fields parameter on the other hand returns only a subset of the fields:
GET /identities/?fields=id,data
[
{
"id": 1,
"data": "John Doe"
},
...
]
And a query with the omit parameter excludes specified fields.
GET /identities/?omit=data
[
{
"id": 1,
"url": "http://localhost:8000/api/identities/1/",
"type": 5
},
...
]
You can use both fields and omit in the same request!
GET /identities/?omit=data,fields=data,id
[
{
"id": 1
},
...
]
Though why you would want to do something like that is beyond this author.
It also works on single objects!
GET /identities/1/?fields=id,data
{
"id": 1,
"data": "John Doe"
}
Usage
When defining a serializer, use the DynamicFieldsMixin:
from drf_dynamic_fields import DynamicFieldsMixin
class IdentitySerializer(DynamicFieldsMixin, serializers.ModelSerializer):
class Meta:
model = models.Identity
fields = ('id', 'url', 'type', 'data')
The mixin needs access to the request object. Some DRF classes like the ModelViewSet set that by default, but if you handle serializers yourself, pass in the request through the context:
events = Event.objects.all()
serializer = EventSerializer(events, many=True, context={'request': request})
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 (see “Usage” section).
There are some cases (e.g. nested serializers) where you cannot get rid of the warning that way (see issue 27). In that case, you can silence the warning through settings.py:
DRF_DYNAMIC_FIELDS = {
'SUPPRESS_CONTEXT_WARNING': True,
}
Testing
To run tests, install Django and DRF and then run runtests.py:
$ python runtests.py
Credits
The implementation is based on this StackOverflow answer. Thanks YAtOff!
The GitHub users X17 and rawbeans provided improvements on my gist that were incorporated into this library. Thanks!
For other contributors, please see Github contributor stats.
License
MIT license, see LICENSE file.
Metadata
Release files for drf-dynamic-fields 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| drf_dynamic_fields-0.4.0.tar.gz | 4.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_dynamic_fields-0.4.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 10.3 kB
Release files / drf_dynamic_fields-0.4.0.tar.gz
| Download URL | drf_dynamic_fields-0.4.0.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f20a5ec27d003db7595c9315db22217493dcaed575f3811d3e12f264c791c20c
|
|
BLAKE2b-256 checksum How to use checksums |
d27f1c17e0791b8d028b47908be78ac8041c544bd07a59afd8556304a0f0d355
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.1 CPython/3.10.4
|
Release files / drf_dynamic_fields-0.4.0-py2.py3-none-any.whl
| Download URL | drf_dynamic_fields-0.4.0-py2.py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
48b879fe899905bc18593a61bca43e3b595dc3431b3b4ee499a9fd6c9a53f98c
|
|
BLAKE2b-256 checksum How to use checksums |
2d184b02bda9ae5a7ac52bfcb4f1740fb4a0aa12d220881695471b700bb3f00e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.1 CPython/3.10.4
|