- Notifications
You must be signed in to change notification settings - Fork31
Dynamically select only a subset of fields per DRF resource, either using a whitelist or a blacklist.
License
dbrgn/drf-dynamic-fields
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation

This package provides a mixin that allows the user to dynamically select only asubset 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+
This library is about filtering fields based on individual requests. It isdeliberately kept simple and we do not plan to add new features (includingsupport for nested fields). Feel free to contribute improvements, codesimplifications and bugfixes though! (See also:#18)
If you need more advanced filtering features, maybedrf-flex-fields could be something for you.
pip install drf-dynamic-fields
Example serializer:
classIdentitySerializer(DynamicFieldsMixin,serializers.HyperlinkedModelSerializer):classMeta:model=models.Identityfields= ('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 ofthe 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"}When defining a serializer, use theDynamicFieldsMixin:
fromdrf_dynamic_fieldsimportDynamicFieldsMixinclassIdentitySerializer(DynamicFieldsMixin,serializers.ModelSerializer):classMeta:model=models.Identityfields= ('id','url','type','data')
The mixin needs access to therequest object. Some DRF classes like theModelViewSet 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})
If the request context does not have access to the request, a warning isemitted:
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 thewarning that way (seeissue 27).In that case, you can silence the warning throughsettings.py:
DRF_DYNAMIC_FIELDS= {'SUPPRESS_CONTEXT_WARNING':True,}
To run tests, install Django and DRF and then runruntests.py:
$ python runtests.py
- The implementation is based onthis StackOverflow answer. Thanks
YAtOff! - The GitHub users
X17andrawbeansprovided improvements onmy gist that were incorporatedinto this library. Thanks! - For other contributors, please seeGithub contributor stats.
MIT license, seeLICENSE file.
About
Dynamically select only a subset of fields per DRF resource, either using a whitelist or a blacklist.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.