Git Product home page Git Product logo

drf-dynamic-serializer's Introduction

DRF Dynamic Serializer

https://badge.fury.io/py/drf-dynamic-serializer.png https://badge.fury.io/py/drf-dynamic-serializer.png https://travis-ci.org/saxix/drf-dynamic-serializer.png?branch=master

Simple plugin for DRF to customise output

Example

Consider this starting code:

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        exclude = ('date_joined', 'password')


class BaseViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

Now add some salt:

class UserSerializerShort(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'first_name', 'last_name')


class DynamicSerializerViewSet(DynamicSerializerMixin, BaseViewSet):
    serializers_fieldsets = {'light': ('last_name', 'first_name'),
                             'short': UserSerializerShort}

this allows queries like:

- /users/?
- /users/?+serializer=std
- /users/?+serializer=light
- /users/?+serializer=short

... and now a bit of pepper:

class DynamicSerializerViewSet(DynamicSerializerMixin, BaseViewSet):
    serializers_fieldsets = {'light': ('last_name', 'first_name'),
                             'short': UserSerializerShort,
                             'dynamic': DynamicSerializer('last_name', 'first_name', 'is_active')}

this allows queries like:

- /users/?
- /users/?+serializer=dynamic&+fields=email,last_name

Links

Stable master-build master-cov  
Development dev-build dev-cov  
Project home page: https://github.com/saxix/drf-dynamic-serializer
Issue tracker: https://github.com/saxix/drf-dynamic-serializer/issues?sort
Download: http://pypi.python.org/pypi/drf-dynamic-serializer/
Documentation: https://drf-dynamic-serializer.readthedocs.org/en/latest/

drf-dynamic-serializer's People

Contributors

saxix avatar gigiusb avatar

Watchers

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