I have this viewset with a custom action added
@permission_classes([IsStaffUser])
class MacroViewSet(viewsets.ModelViewSet):
queryset = Macro.objects.all()
serializer_class = MacroSerializer
http_method_names = ['get', 'post', 'patch', 'head', 'delete']
@action(methods=['post'], detail=True, url_name='add-student')
def add_student(self, request, pk=None):
pass
Registered in urls.py as
# application namespace
app_name = 'appname'
router = routers.DefaultRouter()
router.register(r'users', UserViewSet, basename='user')
router.register(r'students', StudentViewSet)
router.register(r'macro', MacroViewSet, basename='macro')
urlpatterns = [
re_path('^', include(router.urls)),
]
Now, I'm trying to test my custom action. Of course I need its url. Following the drf guide, I've kwnow of the existence of reverse_action
method. In my test case, I use that method as following
self.url = MacroViewSet().reverse_action('add-student', args=['1'])
Unfortunately, I keep getting this error
> self.url = MacroViewSet().reverse_action('add-student', args=['1'])
E AttributeError: 'MacroViewSet' object has no attribute 'basename'