多个对象返回到/api/rentals/gallery/1/

2024-09-30 00:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在GalleryAPI的详细视图中显示多个图像,但我收到一个错误声明

多个对象返回/api/rentals/gallery/1/

get()返回了多个库--它返回了2!

视图.py

class GalleryListAPIView(ListAPIView):
    # queryset = Rental.objects.all()
    serializer_class = GalleryListSerializer
    pagination_class = RentalPageNumberPagination

    def get_queryset(self, *args, **kwargs):
        queryset_list = Gallery.objects.all()
        return queryset_list

class GalleryDetailAPIView(RetrieveAPIView):
    queryset = Gallery.objects.all()
    serializer_class = GalleryDetailSerializer
    lookup_field = 'rental_id'

序列化程序.py

^{pr2}$

Tags: 对象py图像视图声明getobjects错误
1条回答
网友
1楼 · 发布于 2024-09-30 00:22:31

看看documentation

lookup_field - The model field that should be used to for performing object lookup of individual model instances. Defaults to 'pk'.

因为您使用了rental_id,并且使用的是url /api/rentals/gallery/1/,这里的1rental_id,而不是图库的{}。而且,可能有两个gallery对象与rental_id=1相关,这就是为什么在结果中得到这些对象。在

相关问题 更多 >

    热门问题