MultipleObjectsReturned at /api/rentals/gallery/1/
MultipleObjectsReturned at /api/rentals/gallery/1/
我需要在图库的详细视图中显示多张图片 api 但我收到一条错误消息
在 /api/rentals/gallery/1/
处返回了多个对象
get() 返回了不止一个图库 -- 它返回了 2 个!
views.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'
serializers.py
class GalleryListSerializer(ModelSerializer):
class Meta:
model = Gallery
class GalleryDetailSerializer(ModelSerializer):
# image = SerializerMethodField(many=True)
class Meta:
model = Gallery
fields = ('id', 'image', 'rental_id')
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/
这里 1
是 rental_id
而不是 pk
的画廊。而且,可能有两个图库对象与 rental_id=1
相关,这就是为什么您在结果中得到这些对象。
我需要在图库的详细视图中显示多张图片 api 但我收到一条错误消息
在 /api/rentals/gallery/1/
处返回了多个对象get() 返回了不止一个图库 -- 它返回了 2 个!
views.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'
serializers.py
class GalleryListSerializer(ModelSerializer):
class Meta:
model = Gallery
class GalleryDetailSerializer(ModelSerializer):
# image = SerializerMethodField(many=True)
class Meta:
model = Gallery
fields = ('id', 'image', 'rental_id')
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/
这里 1
是 rental_id
而不是 pk
的画廊。而且,可能有两个图库对象与 rental_id=1
相关,这就是为什么您在结果中得到这些对象。