Skip to content
Advertisement

how to reverse the URL of a ViewSet’s custom action in django restframework

I have defined a custom action for a ViewSet

JavaScript

And the viewset is registered to url in the conventional way

JavaScript

The URL /api/users/gender/ works. But I don’t know how to get it using reverse in unit test. (I can surely hard code this URL, but it’ll be nice to get it from code)

According to the django documentation, the following code should work

JavaScript

But I tried the following and they don’t work

JavaScript

In the django-restframework documentation, there is a function called reverse_action. However, my attempts didn’t work

JavaScript

What is the proper way to reverse the URL of that action?

Advertisement

Answer

You can use reverse just add to viewset’s basename action:

JavaScript

See related part of docs.

Advertisement