无法在Django Rest Framework

时间:2015-09-21 22:06:52

标签: django rest django-rest-framework

我努力让HyperlinkedRelated字段正常工作,我无法弄清楚我哪里出错了。无论我做什么,我都会收到如下错误:

  

无法使用视图名称" court-detail"解析超链接关系的URL。您可能未能在API中包含相关模型,或者在此字段上错误地配置了lookup_field属性。

我觉得我已经尝试过一切,但我不知道我的错误在哪里,也不知道如何识别错误。我对此错误消息的阅读是我正在寻找与court-detail视图对应的网址,但该视图并不存在。

某些魔法不起作用,我们将非常感谢任何帮助。

网址:

from cl.api import views

from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'courts', views.CourtViewSet)
router.register(r'dockets', views.DocketViewSet)

router.register(r'clusters', views.OpinionClusterViewSet)
router.register(r'opinions', views.OpinionViewSet)
router.register(r'cited-by', views.OpinionsCitedViewSet)

urlpatterns = [
    # url(r'^api/rest/(?P<version>[v3]+)/', include(router.urls)),
    url(r'^api-auth/',
        include('rest_framework.urls', namespace='rest_framework')),
    url(r'^', include(router.urls)),
]

查看:

class DocketViewSet(viewsets.ModelViewSet):
    queryset = Docket.objects.all()
    serializer_class = serializers.DocketSerializer


class CourtViewSet(viewsets.ModelViewSet):
    queryset = Court.objects.all()
    serializer_class = serializers.CourtSerializer


class OpinionClusterViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.
    """
    queryset = OpinionCluster.objects.all()
    serializer_class = serializers.OpinionClusterSerializer


class OpinionViewSet(viewsets.ModelViewSet):
    queryset = Opinion.objects.all()
    serializer_class = serializers.OpinionSerializer


class OpinionsCitedViewSet(viewsets.ModelViewSet):
    queryset = OpinionsCited.objects.all()
    serializer_class = serializers.OpinionsCitedSerializer

串行器:

from cl.audio import models as audio_models
from cl.search import models as search_models

from rest_framework import serializers


class DocketSerializer(serializers.HyperlinkedModelSerializer):
    court = serializers.HyperlinkedRelatedField(
        many=False,
        view_name='court-detail',
        read_only=True
    )

    class Meta:
        model = search_models.Docket
        fields = ('date_created', 'date_modified', 'date_argued',
                  'date_reargued', 'date_reargument_denied', 'court')


class CourtSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = search_models.Court

class AudioSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = audio_models.Audio


class OpinionClusterSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = search_models.OpinionCluster
        fields = ('judges', 'per_curiam', )


class OpinionSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = search_models.Opinion
        fields = ('pk',)


class OpinionsCitedSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = search_models.OpinionsCited

当我去:

http://127.0.0.1:8000/dockets/

它告诉我上面的错误信息。当然,如果我从序列化中删除court引用,它可以正常工作......

1 个答案:

答案 0 :(得分:1)

我想这可能是由许多事情引起的,但在我的情况下,我发现这是因为设置DEFAULT_VERSIONING_CLASS而没有在urls.py中配置它而引起的:

REST_FRAMEWORK = {
    # Use URL-based versioning
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
    'DEFAULT_VERSION': 'v3',
    'ALLOWED_VERSIONS': {'v3'},
}

因此,解决方案是在设置中禁用它,或者在接受url参数的urls.py中设置version

url(r'^api/rest/(?P<version>[v3]+)/', include(router.urls)),

唉。花了很长时间才意识到我有这个设置。错误信息错误。