action decorator in DRF doesn't work with url_path

Viewed 6578

I'm trying to achive that such endpoint /api/messages/<str:email>/ will return a list of messages for given email.
So in my ViewSet I've created a function with @action decorator

@action(
    detail=False,
    methods=["get"],
    name="Get email messages",
    url_path="<str:email>",
    url_name="Email's messages"
)
def email_messages(self, request, email=None):
    return Response({"aaa": email}, status=200)

But it seems that django doesn't resolve eg. this url: /api/messages/aaa/ (just for the test) and returns 404.

Even if I change url_path to email it returns 404 on /api/messages/email/. It only works when I remove the url_path kwarg from the params. Then it is accessible under /api/messages/email_messages.

How can I achieve a working GET on /api/messages/<str:email/ so that the email will be passed to the function?

1 Answers

It seems DRF does not accept Django path expression but, the regex expression. So, you need to use url_path=r'(?P<email>\w+)' instead of url_path=<str:email>

Also, this path will overwrite the default detail view, that is, /api/messages/<PK>/ API. So, I would recommend using some prefix to this email end-point

@action(
    detail=False,
    methods=["get"],
    name="Get email messages",
    url_path=r'some-prefix/(?P<email>\w+)',
    url_name="Email's messages"
)
def email_messages(self, request, email=None):
    return Response({"aaa": email}, status=200)

now, your API end-point will be, /api/messages/some-prefix/foo-bar/

Related