Enter image description here错误图片屏幕截图
这是我的模型。py:
from django.db import models
class country (models.Model):
country_name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.country_name
class state (models.Model):
state_name = models.CharField(max_length=200, null=True)
country = models.ForeignKey(country, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.state_name
class city (models.Model):
city_name = models.CharField(max_length=200, null=True)
country = models.ForeignKey(country, on_delete=models.CASCADE, null=True)
state = models.ForeignKey(state, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.city_name
class publication(models.Model):
title= models.CharField(max_length=300, null=True)
country=models.ForeignKey(country, on_delete=models.CASCADE, null=True)
state=models.ForeignKey(state, on_delete=models.CASCADE, null=True)
city=models.ForeignKey(city, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.title
这是我的seralizers.py:
from rest_framework import routers, serializers, viewsets
from .models import *
from django.contrib.auth.models import User
from rest_framework import permissions
class publicationSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = publication
fields = '__all__'
这是我的view.py:
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import *
from .serializers import *
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status,mixins,generics
from django.http import Http404
from rest_framework import renderers
class SnippetHighlight(APIView):
# def get(self,request):
def get(self, request):
queryset = publication.objects.select_related('country','state','city')
serializer_class = publicationSerializer(queryset,many=True,
context={'request': request})
return Response(serializer_class.data)
这是我的urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework.urlpatterns import format_suffix_patterns
from api.models import *
from api import views
# from api.serializers import UserSerializer
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^snippets/', views.SnippetHighlight.as_view()),
# url(r'^users/$', views.UserList.as_view()),
# url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
# url(r'^api-auth/', include('rest_framework.urls')),
]
urlpatterns = format_suffix_patterns(urlpatterns)
显示错误:
AssertionError at /snippets/
`HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when instantiating the serializer.
Request Method: GET
Request URL: http://127.0.0.1:8000/snippets/
Django Version: 1.11.15
Exception Type: AssertionError
Exception Value:
`HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when instantiating the serializer.
Exception Location: /home/soubhagya/Desktop/rest/env/local/lib/python2.7/site-packages/rest_framework/relations.py in to_representation, line 356
Python Executable: /home/soubhagya/Desktop/rest/env/bin/python
Python Version: 2.7.12
Python Path:
['/home/soubhagya/Desktop/rest',
'/home/soubhagya/Desktop/rest/env/lib/python2.7',
'/home/soubhagya/Desktop/rest/env/lib/python2.7/plat-x86_64-linux-gnu',
'/home/soubhagya/Desktop/rest/env/lib/python2.7/lib-tk',
'/home/soubhagya/Desktop/rest/env/lib/python2.7/lib-old',
'/home/soubhagya/Desktop/rest/env/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/soubhagya/Desktop/rest/env/local/lib/python2.7/site-packages',
'/home/soubhagya/Desktop/rest/env/lib/python2.7/site-packages']
Server time: Sun, 23 Sep 2018 10:09:39 +0000
它进入外壳,但不进入浏览器
答案 0 :(得分:0)
serializer_class = publicationSerializer(queryset,many=True,
context={'request': request})
正如错误消息明确指出的那样:
HyperlinkedIdentityField
在串行器上下文中需要该请求。在实例化序列化程序时添加context={'request': request}
。
答案 1 :(得分:0)
错误消息确切说明了什么地方。
HyperlinkedIdentityField
需要序列化程序中的请求 上下文。实例化时添加context={'request': request}
序列化器。
Django Rest Framework文档中还有一个HyperlinkedModelSerializer的示例,您可以在其中看到它的使用情况:
serializer = AccountSerializer(queryset, context={'request': request})
在实例化序列化程序时,按照错误消息中的说明添加context={'request': request}
。