项目使用Django Rest Framework时,避免不了安装,配置,遂逐渐补充在这,偶尔用到,便于查找。
pip install djangorestframework
添加到安装:
INSTALLED_APPS = (
...
'rest_framework',
)
权限,筛选,分页配置:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAdminUser',
],
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}
具体app添加serializers.py
from rest_framework import serializers
class ExampleSerializer(serializers.Serializer):