0
votes

Hi im trying to Hyperlink my API but cant seem to get it to work. This is my serializers.py:

from rest_framework import serializers
from api.models import *


class UserSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = UserModel
        fields = '__all__'

        depth = 1


class BlogSerializer(serializers.HyperlinkedModelSerializer):

    posts = serializers.HyperlinkedRelatedField(
        many=True, view_name='blogs-detail', read_only=True)

    class Meta:
        model = BlogModel
        fields = ['url', 'id', 'title', 'body', 'posts']

This is my views.py

class UserViewset(viewsets.ModelViewSet):

    queryset = UserModel.objects.all()
    serializer_class = UserSerializer

             
class BlogViewset(viewsets.ModelViewSet):

    queryset = BlogModel.objects.all()
    serializer_class = BlogSerializer

And this is my urls.py:

from django.urls import path, include
from api.views import *

from rest_framework import routers

router = routers.DefaultRouter()
router.register('users', UserViewset, basename='users')
router.register('blogs', BlogViewset, basename='blogs')

urlpatterns = [
    path('', include(router.urls)),
    path('post/<int:pk>/', PostView, name='post'),
    #path('update-post/<str:pk>/', updatePost, name='post'),
]

Ive tried to follow the django rest tutorial but i still cant seem to get it to work. Im just staring out learingn the rest Framework. Thanks for your feedback in advance!

1
Please include the full error and stack trace. It contains helpful information for debugging.schillingt
Should i include it in the title?user13714701

1 Answers

0
votes

remove basename from router.register
django is not able to find the generated default detail view for that.
https://www.django-rest-framework.org/apiguide/serializers/#hyperlinkedmodelserializer