Как добавить гиперссылку к списку объектов в Django Rest Framework?
У меня такой код:
class EntrySerializer(serializers.HyperlinkedModelSerializer):
comments = serializers.HyperlinkedRelatedField(many=True, view_name='comment-detail', read_only=True)
owner = serializers.HiddenField(default=serializers.CurrentUserDefault())
class Meta:
model = Entry
fields = ['url', 'owner', 'title','comments']
что дает следующее:
HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
...
{
"url": "http://127.0.0.1:7000/api/posts/34476dbd-89fc-420b-8d9e-28f2feb7c193/",
"title": "A Title For This Entry",
"comments": [
"http://127.0.0.1:7000/api/comments/dd1a0f06-c096-494f-a70e-5507f99082e6/"
]
},
...
]
Что я хочу достичь: поле "комментарии" - гиперссылка, ведущая к списку комментариев.
Я пробовал использовать:
comments = serializers.HyperlinkedRelatedField(many=True, view_name='comment-detail',
надеемся получить что-то вроде этого:
"comments": "http://127.0.0.1:7000/api/comments/"
но это приводит к следующей ошибке:
ImproperlyConfigured at /api/posts/
Could not resolve URL for hyperlinked relationship using view name "comment-list". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
Request Method: GET
Request URL: http://127.0.0.1:7000/api/posts/
Django Version: 4.1.3
Exception Type: ImproperlyConfigured
Exception Value:
Could not resolve URL for hyperlinked relationship using view name "comment-list". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
Exception Location: /Users/banana/.virtualenvs/mvp/lib/python3.10/site-packages/rest_framework/relations.py, line 416, in to_representation
Raised during: api.viewsets.EntryViewSet
Python Executable: /Users/banana/.virtualenvs/mvp/bin/python
Python Version: 3.10.2
Python Path:
['/Users/banana/Library/Mobile '
'Documents/com~apple~CloudDocs/repos_x/projects/Duck_take/Backend/django/dev/entries_comments_requirements',
'/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip',
'/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10',
'/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload',
'/Users/banana/.virtualenvs/mvp/lib/python3.10/site-packages']
Server time: Thu, 10 Nov 2022 16:13:02 +0000
Как мне это сделать?