如何让Django Rest Framework中的HyperlinkedModelSerializer适用于外键?

时间:2017-02-06 19:21:58

标签: django django-rest-framework

我是Django Rest Framework的新手,我正在努力让我的序列化能够正常运行两个模型之间的外键关系。我试图将我的设置减少到尽可能简单,但我仍然无法理解它应该如何工作。我试图使用HyperlinkedModelSerializer(来自docs)'它使用超链接来表示关系'。当我尝试访问测试服务器上{model X}的列表或详细视图的url时,我得到:

  

'无法使用视图名称解析超链接关系的URL   " {model Y} -detail"。您可能未能包含相关模型   在您的API中,或者错误地配置了lookup_field属性   这个领域。'

我做错了什么?

我的模特:

from django.db import models


class Project(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField()


class ProjectPhoto(models.Model):
    project = models.ForeignKey(
            Project, related_name='photos', on_delete=models.CASCADE
        )
    image = models.ImageField()
    caption = models.CharField(max_length=100)
    date_added = models.DateTimeField(auto_now_add=True)

我的序列化程序

class ProjectSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Project
        fields = ('name', 'description', 'photos')


class ProjectPhotoSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = ProjectPhoto
        fields = ('image', 'caption', 'date_added', 'project'))

我的观点:

from rest_framework import viewsets

from projects.models import Project, ProjectPhoto
from projects.serializers import ProjectSerializer, ProjectPhotoSerializer


class ProjectViewSet(viewsets.ModelViewSet):
    queryset = Project.objects.all().order_by('name')
    serializer_class = ProjectSerializer


class ProjectPhotoViewSet(viewsets.ModelViewSet):
    queryset = ProjectPhoto.objects.all().order_by('date_added')
    serializer_class = ProjectPhotoSerializer

修改

我的网址:

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

from projects import views


router = routers.DefaultRouter()
router.register(r'^projects', views.ProjectViewSet)
router.register(r'^project-photos', views.ProjectPhotoViewSet)

urlpatterns = [
        url(r'^', include(router.urls)),
    ]

然后将这些添加到我的主应用程序urls.py文件中。我不认为这是问题,好像我将序列化程序更改为ModelSerializer然后一切正常。

1 个答案:

答案 0 :(得分:5)

我认为您的问题在您的urls.py文件中,请参阅代码和图片

rest / urls.py文件

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

from .views import ProjectViewSet, ProjectPhotoViewSet

router = routers.SimpleRouter()
router.register(r'project', ProjectViewSet)
router.register(r'project-photo', ProjectPhotoViewSet)

urlpatterns = [
  url(r'^', include(router.urls)),
]

Principal urls.py文件:

from django.conf.urls import url, include
from django.contrib import admin

from rest import urls as urls_rest

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^rest/', include(urls_rest)),
]

enter image description here

和其他选项,尝试在serializers.py文件中使用此代码:

from rest_framework import serializers


from .models import Project, ProjectPhoto


class ProjectPhotoSerializer(serializers.ModelSerializer):

  class Meta:
    model = ProjectPhoto
    fields = ('image', 'caption', 'date_added', 'project')


class ProjectSerializer(serializers.ModelSerializer):

  class Meta:
    model = Project
    fields = ('name', 'description', 'photos')
    depth = 2
  

您有3个选项可以使用序列化程序(参见下图)   enter image description here