milan
milan

Reputation: 2417

MultipleObjectsReturned at /api/rentals/gallery/1/

I need to show multiple images in detail view of gallery api but i am getting an error stating

MultipleObjectsReturned at /api/rentals/gallery/1/

get() returned more than one Gallery -- it returned 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')

Upvotes: 1

Views: 802

Answers (1)

AKS
AKS

Reputation: 19801

Have a look at the documentation

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

Since you have used rental_id and you are using the url /api/rentals/gallery/1/ here 1 is the rental_id and not the pk of the Gallery. And, it might be that there are two gallery objects related to rental_id=1 and that's why you are getting those in the result.

Upvotes: 1

Related Questions