【问题标题】:DRF: drf-nested-routers: Could not resolve URL for hyperlinked relationship using view nameDRF:drf-nested-routers:无法使用视图名称解析超链接关系的 URL
【发布时间】:2019-01-09 23:44:38
【问题描述】:

我在使用视图集显示模型的 -detail 时遇到问题。我正在为我的网址使用 drf-nested-routers 包。

以下是与课程相关的Courses 和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.

但是,我可以完美地导航到课程的网址 (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() 函数,但对如何处理此错误感到很困惑。我的网址也可能有问题。我是一个初学者,几个小时后我无法弄清楚我做错了什么。感谢您的帮助。

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    修复了无法查看部分详细信息的问题。我已经修改了代码并将 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')
    

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

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 2021-07-10
      • 2021-01-25
      • 2018-11-22
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多