2016-01-29 初稿
ViewSet
没有写serializer_class
属性,而是重写了get_serializer_class()
方法,出现
Cannot use OrderingFilter on a view which does not have either a 'serializer_class' or 'ordering_fields' attribute.
原因:因为启用了rest_framework.filters.OrderingFilter
而没有设置ordering_fields
解决方法:ViewSet
里加ordering_fields
属性,可是禁用rest_framework.filters.OrderingFilter
ViewSet
没有写queryset
属性,而是重写了get_queryset()
方法,出现
'base_name' argument not specified, and could not automatically determine the name from the viewset, as it does not have a '.queryset' attribute.
解决方法:需要在urls.py
里给ViewSet
注册Router
时添加base_name
(base_name
为router
为ViewSet
注册url时自动添加的name前缀,如果未设置则从ViewSet
的queryset
里取,使用ViewSet
自动生成的url name为<base_name>-list <base_name>-detail 等)
urls.py
router.register(r'users', UserViewSet, base_name='user')
给url设置了namespace
urls.py
url(r'^api/', include(router.urls, namespace='api')),
访问部分接口出现
Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
解决方法1:给所有的serializer
里包含的外键字段手动设置view_name
值(注意,继承HyperlinkedModelSerializer
,会隐式添加一个HyperlinkedRelatedField
字段url
,而所有的外键都会变成HyperlinkedRelatedField
字段,所以需要对两种类型字段手动设置view_name
值)
serializers.py
class ContactSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Contact
fields = '__all__'
extra_kwargs = {
'url': {'view_name': 'api:contact-detail'},
'user':{'view_name':'api:user-detail'}
}
解决方法2:启动drf基于NameSpace
的版本控制
settings.py
REST_FRAMEWORK = {
……
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',
……
}