@link decorator in django rest framework as a list

2019-06-25 09:34发布

问题:

so i want to filter all the sales invoices using url .. e.g.

/api/v1/invoice/sales/

i think the best way to do this is by using @link method in the viewset. here is my code ..

@link()
def sales(self, request, pk):
    qs = Invoice.objects.filter(is_sales=True)
    serializer = InvoiceSerializer(qs)
    return Response(serializer.data)

but when i go to '/api/v1/invoice/sales/', and use breakpoints, the script doesn't stop at any point..

what am i doing wrong here?

//mouse

回答1:

the @link() and @action() decorators are only linked to the detail-endpoints (/api/v1/invoice/{pk}/sales/ in your case), see docs.

However, there is a third-party library (drf-extensions) that adds these decorators on a collection level.