简体   繁体   中英

Error: Could not resolve URL for hyperlinked relationship using view name “api:products-list”

I use django-rest-framework . I wanna add url field to each product item using HyperlinkedIdentityField in ProductSerializer but it says there is something wrong. I get an error:
Could not resolve URL for hyperlinked relationship using view name "api:products-list". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.
I don't even know why it can happen, because I specify correct view_name and it should work fine. Thanks for help!

urls.py

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('catalog.urls', namespace='blog')),
url(r'^api/', include('catalog.api.urls', namespace='api')),
]

catalog/api/urls.py

urlpatterns = [
    url(r'^categories/$', CategoryListAPIView.as_view(), name='categories-list'),
    url(r'^products/$', ProductListAPIView.as_view(), name='products-list'),
    url(r'^products/(?P<pk>\d+)/$', ProductDetailAPIView.as_view(), name='product-detail'),
    url(r'^products/(?P<pk>\d+)/edit/$', ProductUpdateAPIView.as_view(), name='product-update'),
    url(r'^products/(?P<pk>\d+)/delete/$', ProductDeleteAPIView.as_view(), name='product-delete'),
    url(r'^categories/(?P<pk>\d+)/$', CategoryDetailAPIView.as_view(), name='category-detail'),
    url(r'^categories/(?P<pk>\d+)/edit/$', CategoryUpdateAPIView.as_view(), name='category-update'),
    url(r'^categories/(?P<pk>\d+)/delete/$', CategoryDeleteAPIView.as_view(), name='category-delete')
]

catalog/api/serializers.py

class ProductSerializer(serializers.ModelSerializer):
    url = serializers.HyperlinkedIdentityField(
        view_name='api:products-list',
        lookup_field='pk',
        )
    category = serializers.SerializerMethodField()

    def get_category(self, obj):
        return str(obj.title)

    class Meta:
        model = Product
        fields = (
            'url',
            'id',
            'title',
            'details',
            'slug',
            'category',
        )


class CategorySerializer(serializers.ModelSerializer):
    products_count = serializers.IntegerField()

    class Meta:
        model = Category
        fields = (
            'id',
            'title',
            'slug',
            'products_count',
        )


class CategoryUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = (
            'title',
        )


class ProductUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = (
            'title',
            'details',
            'category',
        )

catalog/api/views.py

class CategoryListAPIView(ListAPIView):
    queryset = Category.objects.annotate(products_count=Count('product'))
    serializer_class = CategorySerializer
    permission_classes = [AllowAny]
    filter_backends = [SearchFilter, OrderingFilter]
    search_fields = ['title', 'slug']


class CategoryDetailAPIView(RetrieveAPIView):
    queryset = Category.objects.annotate(products_count=Count('product'))
    serializer_class = CategorySerializer
    permission_classes = [AllowAny]


class CategoryDeleteAPIView(DestroyAPIView):
    queryset = Category.objects.annotate(products_count=Count('product'))
    serializer_class = CategorySerializer
    permission_classes = [IsAdminUser]


class CategoryUpdateAPIView(RetrieveUpdateAPIView):
    queryset = Category.objects.annotate(products_count=Count('product'))
    serializer_class = CategoryUpdateSerializer
    permission_classes = [IsAdminUser]


class ProductListAPIView(ListAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = [AllowAny]
    filter_backends = [SearchFilter, OrderingFilter]
    search_fields = ['title', 'details', 'slug', 'category__title']


class ProductDetailAPIView(RetrieveAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = [AllowAny]


class ProductDeleteAPIView(DestroyAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = [IsAdminUser]


class ProductUpdateAPIView(RetrieveUpdateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductUpdateSerializer
    permission_classes = [IsAdminUser]

You have used the wrong url name in the field. You want to mention product-detail instead of product-list .

If you use product-list , then it is trying to map each product item with /products/ with arguments pk of the product. While, product-detail takes argument pk and also is consistent with the logic as each product will have a different url of its own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM