Issue
I have this ModelViewSet class:
class DriveInvoiceViewSet(viewsets.ModelViewSet):
filter_fields = ('location_id', 'logical_deleted')
permission_classes = (UserCanManageFacilityPermission,)
pagination_class = None
def get_serializer_class(self):
...
def get_queryset(self):
...
@action(detail=False, methods=['GET'])
def get_subtotals_by_unit(self, request):
invoices_list = self.filter_queryset(self.get_queryset())
grouped_invoices = get_subtotals_by_unit(invoices_list)
return Response(grouped_invoices)
How can I get the URL from reverse function to test the get_subtotals_by_unit action?
The ViewSet registred by the router router.register('drive_invoices', DriveInvoiceViewSet, base_name='drive_invoices')
Solution
Change the action
decorator slightly as below,
class DriveInvoiceViewSet(viewsets.ModelViewSet):
# other code
@action(detail=False, methods=['GET'], url_path="/some/path/", url_name="some-view-name")
def get_subtotals_by_unit(self, request):
invoices_list = self.filter_queryset(self.get_queryset())
grouped_invoices = get_subtotals_by_unit(invoices_list)
return Response(grouped_invoices)
Thus, DRF will create a url pattern with a view name with a syntax as <router_base_name>-<action_view_name>
Thus, the view name in your case will be, drive_invoices-some-view-name
Answered By - JPG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.