Mohammad Donyavi
Mohammad Donyavi

Reputation: 23

Django Rest when i replace ModelSerializer with HyperlinkedModelSerializer, Raise this error

In my django rest project when I replace ModelSerializer with HyperlinkedModelSerializer, this error Raise below;

' Could not resolve URL for hyperlinked relationship using view name "product-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field. '


# Views.py

class ProductView(viewsets.ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer


class CatView(viewsets.ModelViewSet):
    queryset = SubCategory.objects.all()
    serializer_class = CatSerializer


class BrandView(viewsets.ModelViewSet):
    queryset = Brand.objects.all()
    serializer_class = BrandSerializer

# Serializer.py

class BrandSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Brand
        fields = '__all__'

class CatSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SubCategory
        fields = '__all__'

class ProductSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Product
        fields = '__all__'

# urls.py

app_name = 'api'

router = routers.DefaultRouter()
router.register('product', views.ProductView)
router.register('category', views.CatView)
router.register('brand', views.BrandView)

Upvotes: 2

Views: 266

Answers (1)

saeed Rafieyan
saeed Rafieyan

Reputation: 86

you should just remove appname = "api" from your urls

Upvotes: 4

Related Questions