Coder Social home page Coder Social logo

drf-fsm-transitions's Introduction

drf-fsm-transitions

Automatically hook your Django-FSM transitions up to Django REST Framework

Installation

pip install drf-fsm-transitions

Usage

When declaring your viewset, simply mix in the result of get_viewset_transition_action_mixin

from rest_framework import viewsets
from drf_fsm_transitions.viewset_mixins import get_viewset_transition_action_mixin

from .models import Article


class ArticleViewSet(
    get_viewset_transition_action_mixin(Article),
    viewsets.ModelViewSet
):
    queryset = Article.objects.all()

if Article had 2 transitions, delete and publish, the following API calls would be set up

  • POST /api/article/1234/delete/
  • POST /api/article/1234/publish/

Custom route arguments

Passing arguments to the @detail_route decorator can be done by specifiying them in the get_viewset_transition_action_mixin method:

class ArticleViewSet(
    get_viewset_transition_action_mixin(Article, permission_classes=[...]),
    viewsets.ModelViewSet
):
    queryset = Article.objects.all()

This will set permission_classes on each @detail_route for all transitions. There is currrently no way to specify individual arguments for each transition.

Saving

By default, the model instance will be saved after the transition has been successfully called. This can be disabled with the save_after_transition attribute

class ArticleViewSet(
    get_viewset_transition_action_mixin(Article),
    viewsets.ModelViewSet
):
    queryset = Article.objects.all()
    save_after_transition = False

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.