gpt4 book ai didi

python - Django Rest Framework 3.0 to_representation 未实现

转载 作者:太空狗 更新时间:2023-10-30 00:29:44 27 4
gpt4 key购买 nike

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

谢谢。

最佳答案

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

updated default pagination serializer使用 ReadOnlyField 作为 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'

关于python - Django Rest Framework 3.0 to_representation 未实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27462330/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com