Изменить url действия drf
I have an action named image
on a viewset that serve an image dynamically. Is it possible to make url like path/to/api-point/3/image.jpg
instead of path/to/api-point/3/image
?
Я нашел решение. Декоратор действия получает url_path в качестве аргумента.
@action(detail=True, methods=['get'], url_path='image.jpg')
def image(self, request, pk=None):
instance = self.get_object()
if instance.owner == request.user:
response = HttpResponse(status=200)
response['Content-Type'] = ''
response['X-Accel-Redirect'] = '/protected/' + instance.image.url
return response
else:
raise rest_framework.exceptions.PermissionDenied()