Giter Site home page Giter Site logo

josericardopenase / django-dynamic-viewsets Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 24 KB

Django app for making rest_framework dynamic viewsets. You can change the fields of the serializer depending on which action are you doing in the view.

License: GNU General Public License v3.0

Python 100.00%

django-dynamic-viewsets's Introduction

Dynamic django viewsets for drf

What are dynamic views

Dynamic views is a efficient way to reduce your amount of serializers.This is achieved because through the viewset that django_dynamic_fields provides, the fields can be selected depending on the action that is being performed

example:

class ArticleViewSet(DynamicViewSet):
	queryset = Article.objects.all()
	serializer_class = ArticleSerializer
	action_fields = {
		'list' : ('id', ,'title', 'pub_date', 'excerpt'),
		'retrieve': ('id', 'title', 'pub_date', 'text', 'author'),
	}

Features and advantage

  • Create your own viewsets using concrete fields per action.
  • Create serializers with dynamic fields
  • Reusable components.
  • Less amount of serializers

Tutorial:

Start using django_dynamic_fields is so easy. First we must create our custom serializer and make it inherit from DynamicFieldsModelSerializer.

class ArticleSerializer(DynamicFieldsModelSerializer):
	class Meta:
		model = Article
		fields = '__all__'

Easy as that. Then we must create our custom viewset like the example before.

class ArticleViewSet(DynamicViewSet):
	queryset = Article.objects.all()
	serializer_class = ArticleSerializer
	action_fields = {
		'list' : ('id', ,'title', 'pub_date', 'excerpt'),
		'retrieve': ('id', 'title', 'pub_date', 'text', 'author'),
	}

And thats all!! Happy hacking :)

django-dynamic-viewsets's People

Contributors

josericardopenase avatar

Stargazers

Aris Winandi avatar

Watchers

 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.