首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DRF: drf嵌套路由器:无法使用视图名称解析超链接关系的URL

DRF: drf嵌套路由器:无法使用视图名称解析超链接关系的URL
EN

Stack Overflow用户
提问于 2019-01-09 19:20:31
回答 1查看 429关注 0票数 0

我在使用视图集显示模型的-detail时遇到问题。我对我的urls使用了drf-nested-routers包。

以下是与本课程相关的CourseSection的示例。(代码如下)

代码语言:javascript
复制
[
    {
        "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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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的问题。我是一个初学者,过了很长时间我都不知道我做错了什么。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-01-09 23:34:15

修复无法查看部分详细信息的问题。我修改了代码,并将url字段修改为SectionSerializer,如下所示:

代码语言:javascript
复制
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')

并且现在能够导航所有端点。我不知道为什么,所以我真的很感谢你对这种行为的解释。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54108986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档