The API returns 404 to my GET request:

My API:

class ProductSerializer(serializers.ModelSerializer): color = ColorSerializer() class Meta: model = models.Product fields = ( 'id', 'title', 'short_title', 'get_absolute_url', 'get_cart_thumbnail', 'get_related_thumbnail', 'part_number', 'price', 'color', 'get_weight_in_g', 'get_volume_in_m3', 'stock_spb' ) @permission_classes((permissions.AllowAny,)) class ProductInfo(APIView): @staticmethod def get(request): product = models.Product.objects.filter(slug=request.split('/')) s_product = CitySerializer(product, many=False) return Response(s_product.data) 

My router:

 router = DefaultRouter() router.register(r'product', rest.ProductInfo, 'product') urlpatterns = router.urls 

The idea is for the API to accept requests of the form:

 http://127.0.0.1:8000/api/catalog/product/slug-of-a-product/ 

What am I doing wrong?

  • 2
    Why are you using APIView, not ViewSet? Maybe because of this, the router cannot parse the url and view parvilno? - Chikiro 2:55 pm

2 answers 2

Judging by the code (and a little on the documentation ) you forgot lookup_field. Lay out the serializer for fidelity, I will try to run

  • one
    True, it was a matter of lookup_field as lookup_field , but my main mistake was that I should have used ViewSet as suggested by @Chikiro - Viktor

With routs everything was right. Here is a class that does everything that I wanted.

 @permission_classes((permissions.AllowAny,)) class ProductInfo(RetrieveModelMixin, viewsets.GenericViewSet): queryset = models.Product.objects.all() lookup_field = "slug" serializer_class = ProductSerializer