我有一个ViewSet
的
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)
用router.register(r'customers', views.CustomerViewSet)
捆绑到urls.py
我可以GET /api/customers
和GET /api/customers/1000
,但找不到{404
。流从不进入licenses
方法。在
我在这里看到了类似的问题,但是他们使用了一个不正确的签名,我没有:def licenses(self, request, pk=None)
编辑:再一次,我问了不到一分钟就找到了我的答案。。。显然,装饰器需要像@detail_route()
这样的括号。我以为这些都是按惯例选择的。。?在
对于
post
请求,您需要指定方法,因为默认情况下,detail_route
装饰器将路由get
请求。 像这样:@detail_route(methods=['POST'])
默认情况下,router会在url中添加尾部斜杠。因此,
GET /api/customers/
将代替GET /api/customers
工作。如果不想使用尾随斜杠,可以将trailing_slash = False
传递给路由器初始值设定项。在例如-
如果这不起作用,那么您将路由器url导入主url的方式就有问题了。在
相关问题 更多 >
编程相关推荐