Marek
Marek

Reputation: 1249

action decorator in DRF doesn't work with url_path

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?

Upvotes: 12

Views: 15317

Answers (2)

xalien1
xalien1

Reputation: 121

i had this problem and regex expression didn't work i don't know why the whole Extra action part vanish when using url_path in regex and don't work when using Django path in action anyway for now i added the path like maybe someone can explain it or anyone like me can use this solution:

urlpatterns = [path( "getattributes/<slug:product_class_slug>/", ClassViewSet.as_view({"get": "get_class_attribute"}),), ] + router.urls

Upvotes: 0

JPG
JPG

Reputation: 88649

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/

Upvotes: 20

Related Questions