'get() returned more than one Product -- it returned 2 Django Rest Framework
DRF returning this: get() returned more than one Product -- it returned 2!, when im trying to get objects from my DB by PK
Serializers
class ProductSerializer(serializers.ModelSerializer):
# cat_id = serializers.SlugRelatedField(slug_field='cat_id', read_only=True)
class Meta:
model = Product
fields = ('name', 'description', 'cat_id', 'use', 'diametr', 'len', 'color', 'photo')
Views
class CategoryProductView(APIView):
def get(self, request, pk):
product = Product.objects.get(cat_id=pk)
serializer = ProductSerializer(product)
return Response(serializer.data)
Urls
path('api/categorylist/<int:pk>', CategoryProductView.as_view())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
