Django Rest Framework 3.0 to_representation未实现

时间:2014-12-13 18:42:20

标签: python django django-rest-framework

我使用Django 1.7.1和Python 2.7从Django Rest Framework 2.4升级到3.0.1,并且无法解决以下错误:

File "/Users/bjacobel/.virtualenvs/hey/lib/python2.7/site-packages/rest_framework/fields.py", line 375, in to_representation
    raise NotImplementedError('to_representation() must be implemented.')

我使用的代码在2.4下运行得很好,我很难找到关于我正在使用的DRF类中发生了哪些变化的文档。我评论了除了我的一个端点之外的所有端点(为django.contrib.auth.models.User提供CRUD的端点,我仍然得到错误。

serializers.py

from django.contrib.auth.models import User
from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'first_name', 'last_name', 'email', 'username')

views.py

from django.contrib.auth.models import User
from hey.apps.api import serializers
from rest_framework import viewsets, permissions, filters

class User(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = serializers.UserSerializer
    permission_classes = (permissions.IsAuthenticated,)
    filter_backends = (filters.OrderingFilter,)

urls.py

from django.conf.urls import patterns, url, include
from hey.apps.api import views
from rest_framework.routers import SimpleRouter

router = SimpleRouter()


router.register(r'user', views.User)

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

pagination.py

from rest_framework import pagination
from rest_framework import serializers

class LinksSerializer(serializers.Serializer):
    next = pagination.NextPageField(source='*')
    prev = pagination.PreviousPageField(source='*')

class CustomPaginationSerializer(pagination.BasePaginationSerializer):
    links = LinksSerializer(source='*')  # Takes the page object as the source
    total_results = serializers.Field(source='paginator.count')

    results_field = 'objects'

settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_SERIALIZER_CLASS': 'hey.apps.api.pagination.CustomPaginationSerializer',
    'PAGINATE_BY': 20,                  # Default to 20
    'PAGINATE_BY_PARAM': 'limit',       # Allow client to override, using `?limit=xxx`.
    'MAX_PAGINATE_BY': 100,             # Maximum limit allowed when using `?limit=xxx`.
    'TEST_REQUEST_DEFAULT_FORMAT': 'json',
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
}

谢谢。

1 个答案:

答案 0 :(得分:13)

问题在于您的分页序列化程序,因为您在Django REST Framework 3.0中使用的serializers.Field现在是serializers.ReadOnlyField。这是一个微妙的变化,虽然它在发布公告中被提及,但对于那些超越分页序列化器的人来说,它是最引人注目的。

updated default pagination serializerReadOnlyField字段使用count。您应该可以通过更换字段来修复序列化程序。

class CustomPaginationSerializer(pagination.BasePaginationSerializer):
    links = LinksSerializer(source='*')  # Takes the page object as the source
    total_results = serializers.ReadOnlyField(source='paginator.count')

    results_field = 'objects'