DRF:drf-nested-routers:无法使用视图名称解析超链接关系的URL

时间:2019-01-09 11:20:31

标签: django django-rest-framework

我无法使用视图集显示模型的-detail。我的网址使用的是drf-nested-routers软件包。

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

[
    {
        "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()函数,但是对于如何解决这个错误。我的网址也可能有问题。我是一个初学者,很多小时后我无法弄清楚自己在做什么。谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

修复了无法查看部分详细信息的问题。我摆弄了代码,然后将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')

,现在可以浏览所有端点。我不确定为什么,所以我非常感谢对此行为的解释。