Odd behavior with the @action decorator

356 views
Skip to first unread message

Chris Niedzwiedz

unread,
May 18, 2013, 8:35:39 AM5/18/13
to django-res...@googlegroups.com
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)

Chris Niedzwiedz

unread,
May 18, 2013, 10:57:01 AM5/18/13
to django-res...@googlegroups.com
Guess I should have checked this first, but updating to latest fixed this problem for me.  Thanks anyway!

Steve Hiemstra

unread,
Jun 13, 2013, 11:44:08 AM6/13/13
to django-res...@googlegroups.com, cnied...@gmail.com
How did you get the @actions to display in the View?  Do they show up on the detail view only?  I can't get them to appear anywhere... (but they still work if try to go to objects/pk/action_name)
Reply all
Reply to author
Forward
0 new messages