Code Monkey home page Code Monkey logo

drf-dynamic-fields's Introduction

Dynamic Serializer Fields for Django REST Framework

Build status

PyPI Version

PyPI Downloads

License is MIT

This package provides a mixin that allows the user to dynamically select only a subset of fields per resource.

Installing

pip install drf-dynamic-fields

What It Does

Example serializer:

python

class IdentitySerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer):
class Meta:

model = models.Identity fields = ('id', 'url', 'type', 'data')

A regular request returns all fields:

GET /identities

json

[
{

"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

json

[
{

"id": 1, "data": "John Doe"

},

]

And a query with the omit parameter excludes specified fields.

GET /identities/?omit=data

json

[
{

"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

json

[
{

"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

json

{

"id": 1, "data": "John Doe"

}

Usage

When defining a serializer, use the DynamicFieldsMixin:

python

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:

python

events = Event.objects.all() serializer = EventSerializer(events, many=True, context={'request': request})

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. 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.

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.

drf-dynamic-fields's People

Contributors

dbrgn avatar jtrain avatar michael-k avatar sbchisholm avatar

Watchers

James Cloos avatar Artur Barseghyan avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.