【问题标题】:Django Rest Framework 3.0 to_representation not implementedDjango Rest Framework 3.0 to_representation 未实现
【发布时间】:2014-12-13 18:42:20
【问题描述】:

我正在使用 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)),
)

分页.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',
    )
}

谢谢。

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    问题出在您的分页序列化程序中,因为您在 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'
    

    【讨论】:

    猜你喜欢
    • 2015-06-29
    • 2016-03-15
    • 2015-07-04
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多