django中出现错误(all()获得意外的关键字参数'id')

guz6ccqo  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(318)

我正在尝试使用django rest框架制作api
URL.py:

urlpatterns = [
    path('', views.eventList, name="events"),
    path('detail/<str:pk>/', views.eventDetail, name="detail"),
    path('create', views.eventCreate, name="create"),
    path('update/<str:pk>/', views.eventUpdate, name="update"),
    path('delete/<str:pk>/', views.eventDelete, name="delete")
]

views.py:

@api_view(['GET'])
def eventList(request):
    events = Orders.objects.all()
    serializer = OrdersSerializer(events, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def eventDetail(request, pk):
    events = Orders.objects.all(id=pk)
    serializer = OrdersSerializer(events, many=True)
    return Response(serializer.data)

当我想访问 http://127.0.0.1:8000/api/events/detail/1/ 给出了这个错误。但是 http://127.0.0.1:8000/api/events/ 还在工作。
而且我在搜索这个错误时也没有找到正确的解决方案
p、 我用inspectdb制作我的models.py

6bc51xsx

6bc51xsx1#

您需要从eventdetail视图中的order queryset中删除id=pk。

@api_view(['GET'])
def eventDetail(request, pk):
    events = Orders.objects.all() # here
    # or in case you need to filter by pk
    events = Orders.objects.filter(pk=pk)

    serializer = OrdersSerializer(events, many=True)
    return Response(serializer.data)

相关问题