Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework @detail_route yields 404

I have a ViewSet like

class CustomerViewSet(ModelViewSet):
    queryset = models.Customer.objects.all()
    serializer_class = serializers.CustomerSerializer
    filter_class = filters.CustomerFilterSet

    @detail_route
    def licenses(self, request, pk=None):
        customer = self.get_object()
        licenses = Item.objects.active().sold().for_customer(customer)
        serializer = serializers.ItemSerializer(licenses, many=True)
        return Response(serializer.data)

Strapped into urls.py with router.register(r'customers', views.CustomerViewSet)

i can GET /api/customers and GET /api/customers/1000, but GET /api/customers/1000/licenses is not found. It gives me 404. The flow never enters the licenses method.

I looked at similar questions here, but they used an incorrect signature, which I do not: def licenses(self, request, pk=None)

python 3.4.0
djangorestframework==3.2.3

EDIT: Once again, I find my answer less than a minute after asking... Apparantly, the decorater needs parenthesees like @detail_route(). I thought these were always optional by convention..?

like image 786
Eldamir Avatar asked Jan 18 '26 22:01

Eldamir


1 Answers

For a post request you'll need to specify the method because the detail_route decorator will route get requests by default. Like so: @detail_route(methods=['POST'])

like image 59
Erick M Avatar answered Jan 21 '26 10:01

Erick M