问题描述
错误图片截图
这是我的models.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
它是在 shell 中,但不是在浏览器中
1楼
serializer_class = publicationSerializer(queryset,many=True,
context={'request': request})
正如错误消息清楚地表明:
HyperlinkedIdentityField
需要序列化程序上下文中的请求。 实例化序列化程序时添加context={'request': request}
。
2楼
错误消息准确说明出了什么问题。
HyperlinkedIdentityField
需要序列化程序上下文中的请求。 实例化序列化程序时添加context={'request': request}
。
Django Rest Framework 文档中还有一个示例,您可以在其中看到它的使用情况:
serializer = AccountSerializer(queryset, context={'request': request})
如错误消息所述,在实例化序列化程序时添加context={'request': request}
。