我在使用视图集显示模型的-detail时遇到问题。我对我的urls使用了drf-nested-routers包。
以下是与本课程相关的Course和Section的示例。(代码如下)
[
{
"url": "http://127.0.0.1:8000/courses/CS250/",
"course_code": "CS250",
"sections_offered": [
"http://127.0.0.1:8000/courses/CS250/sections/01/"
]
},
{
"url": "http://127.0.0.1:8000/courses/CS150/",
"course_code": "CS150",
"sections_offered": []
}
]只有在什么都没有的情况下,我才能导航到courses/CS250/sections,但是一旦我在那里创建了一个对象,我就不能再访问那个端点,我也不能访问该对象的URL (http://127.0.0.1:8000/courses/CS250/sections/01/),除非得到这个错误:django.core.exceptions.ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "section-detail". You may have failed to include the related model in your API, or incorrectly configured the 'lookup_field' attribute on this field.
但是,我可以很好地导航到课程的url (http://127.0.0.1:8000/courses/CS250/)
/models.py
from django.db import models
class Course(models.Model):
course_code = models.CharField(default='', max_length=50, primary_key=True)
class Section(models.Model):
section_number = models.CharField(default='01', max_length=100, primary_key=True)
parent_course = models.ForeignKey(Course, related_name="sections_offered", on_delete=models.CASCADE, blank=True, null=True)/views.py
from . import models
from . import serializers
from rest_framework import viewsets
class CourseViewSet(viewsets.ModelViewSet):
serializer_class = serializers.CourseSerializer
queryset = models.Course.objects.all()
class SectionViewSet(viewsets.ModelViewSet):
serializer_class = serializers.SectionSerializer
queryset = models.Section.objects.all()/serializers.py
from rest_framework_nested.relations import NestedHyperlinkedRelatedField
from rest_framework import serializers
from . import models
class CourseSerializer(serializers.HyperlinkedModelSerializer):
sections_offered = NestedHyperlinkedRelatedField(
many=True,
read_only=True,
view_name='section-detail',
parent_lookup_kwargs={'parent_course_pk': 'parent_course__pk'}
)
class Meta:
model = models.Course
fields = ("url", "course_code", "sections_offered",)
class SectionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Section
fields = "__all__"/urls.py
from rest_framework import routers
from . import views
from rest_framework_nested.routers import NestedSimpleRouter
from django.conf.urls import url, include
router = routers.DefaultRouter()
router.register(r'courses', views.CourseViewSet)
courses_router = NestedSimpleRouter(router, r'courses', lookup='parent_course')
courses_router.register(r'sections', views.SectionViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^', include(courses_router.urls)),
]我认为SectionViewSet可能有问题,我可能需要覆盖视图集的retrieve()和list()函数,但我对如何处理这个bug感到相当困惑。这也可能是我的urls的问题。我是一个初学者,过了很长时间我都不知道我做错了什么。谢谢你的帮助。
发布于 2019-01-09 23:34:15
修复无法查看部分详细信息的问题。我修改了代码,并将url字段修改为SectionSerializer,如下所示:
class SectionSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedRelatedField(
read_only=True,
view_name='section-detail'
)
class Meta:
model = models.Section
fields = ('url', 'section_number', 'parent_course')并且现在能够导航所有端点。我不知道为什么,所以我真的很感谢你对这种行为的解释。
https://stackoverflow.com/questions/54108986
复制相似问题