我有这样编码的串行器:
class InternationalSerializer(serializers.Serializer):
""" Serializer for International
Serializes version, which is displayed on the
International page
"""
overall_version = serializers.SerializerMethodField('get_overall_version',
read_only=True)
def get_overall_version(self):
# sum up all the individual country versions
# to keep a unique value of the overall
# International version
sum_of_versions = 0
for key in constants.country_versions:
sum_of_versions+=key.value()
return sum_of_versions
〜
现在,我希望通过views.py文件显示InternationalSerializer类的'overall_version'。这是我的代码:
class International(generics.GenericAPIView):
serializer_class = InternationalSerializer()
每当我尝试加载/ domain / international /时,我都会得到405 Method not allowed错误。 这是我的urls.py包含的内容:
urlpatterns = patterns('',
url(r'^international/$', views.International.as_view()), ...
这可能是什么问题? 谢谢!
答案 0 :(得分:0)
在您的情况下,您似乎并不真正需要序列化程序,因为您不会对任何对象(无论是python还是django模型对象)进行操作
因此,您可以直接返回响应,而不是使用序列化程序:
from rest_framework import generics
from rest_framework.response import Response
class International(generics.GenericAPIView):
def get(self, request, *args, **kwargs):
sum_of_versions = 0
for key in constants.country_versions:
sum_of_versions+=key.value()
return Response({'sum_of_versions': sum_of_versions})
您获得405的原因是您没有在通用API视图类上指定get
方法。