Django Rest Framework - business action specific views? -
i have basic model based view uses model serializer:
class actionitemtextserializer(serializers.modelserializer): assignee_name = serializers.charfield(source='get_assignee_name') class meta: model = actionitem fields = ('id', 'created_by', 'created_date', 'project', 'portfolio', 'name', 'description', 'parent', 'priority', 'status', 'assignee', 'assignee_name', 'wf_get_actions') #depth = 1 class actionitemviewset(viewsets.modelviewset): queryset = actionitem.objects.all() serializer_class = actionitemtextserializer
so when go /actionitems/
list of them , when go /actionitems/5/
details individual action item.
my action items can have specific actions associated them - how go extending have following:
get /actionitems/5/assign
, model view action item id=5 additional data (i can add via view's serializer suppose)
put /actionitems/5/assign
, trigger view update model data put data , additional change based on action key ('assign') passed it?
can somehow extend modelviewset can return different serializer , perform different actions while put/post etc based on parameter after /actionitems/5/? or should use different approach here.
django rest framework allows add "actions" viewset through @detail_route
decorator. can read more decorator in documentation viewsets , requires use of built-in routers.
in order support multiple request methods (put
/post
), going need pass them in through methods
argument decorator. using
@detail_route(methods=['post', 'put'])
you can route based on method being used checking request.method
on request
passed in.
Comments
Post a Comment