I'm experiencing some weird behavior with the @action decorator when adding new methods to a viewset. First off, if I don't include parens like: @action() I get the following error:
File "/home/cniedzwiedz/Development/finance/trading_agent_web/stasisapi/views.py", line 124, in SimulationViewSet
def results(self, request, pk=None):
TypeError: action() takes exactly 0 arguments (1 given)
Adding parens will make this go away, but then the framework seems to ignore any subsequent custom actions. In the below snippet, only the run() method appears to be incorporated in the URLs list via the router. the results() method Does not error, but no results URL is created and any reverse lookup fails. Any thoughts? Seen this before?
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import action
import models
from .serializers import SimulationSerializer
class SimulationViewSet(viewsets.ModelViewSet):
"""
Viewset for dealing with the simulations over the API. All write methods
are overridden to properly handle the input data.
"""
model = models.Simulation
serializer_class = SimulationSerializer
@action()
def run(self, request, pk=None):
"""
Trigger the running of the simulation
"""
return Response(status = status.HTTP_200_OK)
@action()
def results(self, request, pk=None):
"""
Retrieve the results for the given simulation pk
"""
return Response(status = status.HTTP_200_OK)