【发布时间】:2020-02-26 19:07:14
【问题描述】:
我需要通过使用 Django rest 框架调用 HTTP 请求来检索一组过滤的数据。
这是我的 API 代码:
urls.py
urlpatterns = [
path('api/get_products/', views.get_products),
]
Views.py
@api_view(["GET", ])
def get_products(request):
category_name = request.data['category_name']
category_obj = Category.objects.get(name=category_name)
products_list = Product.objects.filter(category=category_obj)
serializer = ProductSerializers(products_list)
return Response(serializer.data)
最后是 serialierz.py
class CategorySerializers(serializers.HyperlinkedModelSerializer):
class Meta:
model = Category
fields = ['name', 'id']
class ProductSerializers(serializers.HyperlinkedModelSerializer):
category = CategorySerializers()
class Meta:
model = Product
fields = '__all__'
并尝试使用带有参数的 get 请求调用它:{'category_name':'the_name_of_the_category' }
它返回此错误:
KeyError at /categories/api/api/get_products/
'category_name'
【问题讨论】:
标签: python django django-rest-framework httprequest