I have a Serializer coded this way:
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
~
Now, I wish to display the 'overall_version' of the InternationalSerializer class through the views.py file. Here's my code:
class International(generics.GenericAPIView):
serializer_class = InternationalSerializer()
Whenever I try to load /domain/international/, I get 405 Method not allowed error. Here's what my urls.py contains:
urlpatterns = patterns('',
url(r'^international/$', views.International.as_view()), ...
What could be the issue here? Thanks!
Seems like in your case you don't really need serializer, since you don't operate on any object (either it's python or django model object)
So instead of using serializer you can return response directly:
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})
The reason you get 405 was that you have not specified get
method on your generic api view class.