我正在创建一个rest API,我想要一个带有查询参数的网址,如下所示:
http://127.0.0.1:8000/API/prices?portion=small
为此,我对official docs进行了愚弄:“根据查询参数进行过滤”
这是我的观点:
class PurchaseList(generics.ListAPIView):
serializer_class = PurchaseSerializer
def get_queryset(self):
queryset = Purchase.objects.all()
portion = self.request.query_params.get('portion', None)
if portion is not None:
if portion == "small":
queryset = queryset.filter(portion=False)
elif portion == "large":
queryset = queryset.filter(portion=True)
return queryset
在我的urls.py中,我正在创建路由器并注册如下所示的视图:
router = routers.DefaultRouter()
router.register(r'prices', views.PurchaseList, base_name='subprices')
urlpatterns = [
path('API/', include(router.urls)),
...
]
但是,这会在runserver
上产生以下错误:
AttributeError: type object 'PurchaseList' has no attribute 'get_extra_actions'
我已经设法使api正常工作,而无需再次在其docs之后进行任何查询。我该如何解决。我确实在router.register()
或view