From f0975c65056da1177e5d043263f121ed6e55c147 Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Sat, 8 Jan 2022 06:56:58 +0530 Subject: [PATCH] hyperlinked relations --- website/snippets/serializers.py | 7 ++++--- website/snippets/urls.py | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/website/snippets/serializers.py b/website/snippets/serializers.py index b3d87c8..2bb7e49 100644 --- a/website/snippets/serializers.py +++ b/website/snippets/serializers.py @@ -3,15 +3,16 @@ from django.contrib.auth.models import User from rest_framework import serializers from .models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES -class SnippetSerializer(serializers.ModelSerializer): +class SnippetSerializer(serializers.HyperlinkedModelSerializer): owner = serializers.ReadOnlyField(source='owner.username') + highlight = serializers.HyperlinkedIdentityField(view_name='snippet-highlight', format='html') class Meta: model = Snippet - fields = ['id', 'title', 'owner', 'code', 'linenos', 'language', 'style'] + fields = ['url', 'id', 'highlight', 'owner', 'title', 'code', 'linenos', 'language', 'style'] class UserSerializer(serializers.ModelSerializer): snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all()) class Meta: model = User - fields = ['id', 'username', 'snippets'] \ No newline at end of file + fields = ['url', 'id', 'username', 'snippets'] \ No newline at end of file diff --git a/website/snippets/urls.py b/website/snippets/urls.py index 52cd7b8..dfc25de 100644 --- a/website/snippets/urls.py +++ b/website/snippets/urls.py @@ -4,11 +4,21 @@ from . import views urlpatterns = [ path('', views.api_root), - path('snippets/', views.SnippetList.as_view()), - path('snippets//', views.SnippetDetail.as_view()), - path('users/', views.UserList.as_view()), - path('users//', views.UserDetail.as_view()), - path('snippets//highlight/', views.SnippetHighlight.as_view()), + path('snippets/', + views.SnippetList.as_view(), + name='snippet-list'), + path('snippets//', + views.SnippetDetail.as_view(), + name='snippet-detail'), + path('snippets//highlight/', + views.SnippetHighlight.as_view(), + name='snippet-highlight'), + path('users/', + views.UserList.as_view(), + name='user-list'), + path('users//', views.UserDetail.as_view(), + name='user-detail'), + ] urlpatterns = format_suffix_patterns(urlpatterns) \ No newline at end of file