10

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 Answer 1

18

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/

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.