1.DRF框架的8个核心功能
1.认证(用户登录校验用户名密码或者token是否合法)
2.权限(根据不同的用户角色,可以操作不同的表)
3.限流(限制接口访问速度)
4.序列化(返回json)
5.分页
6.版本(接口版本号,用 v1/v2/v3)
# api.example.com/v1/login # 只有用户名密码登录
# api.example.com/v2/login # 手机号,微信 登录
7.过滤(username=zhangsan)
8.排序(ordering=-id)
2.相关包
'''1.序列化相关'''
serializer
ModelSerializer
'''2.DRF视图函数继承'''
APIView
ModelViewSet
1.1 安装DjangoRestFramework
pip install djangorestframework==3.11.1
pip install django-filter==2.3.0 # 过滤器
pip install markdown # Markdown support for the browsable API.
1.2 在syl/settings.py中注册
INSTALLED_APPS = [
'django_filters',
'rest_framework',
]
1.3 syl/settings.py配置DRF: 全局配置
REST_FRAMEWORK = {
# 文档报错: AttributeError: ‘AutoSchema’ object has no attribute ‘get\_link’
# 用下面的设置可以解决
'DEFAULT\_SCHEMA\_CLASS': 'rest\_framework.schemas.AutoSchema',
# 默认设置是:
# 'DEFAULT\_SCHEMA\_CLASS': 'rest\_framework.schemas.openapi.AutoSchema',
# 异常处理器
# 'EXCEPTION\_HANDLER': 'user.utils.exception\_handler',
# Base API policies
'DEFAULT\_RENDERER\_CLASSES': \[
'rest\_framework.renderers.JSONRenderer',
'rest\_framework.renderers.BrowsableAPIRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
],
'DEFAULT_AUTHENTICATION_CLASSES': [
# 'rest_framework.authentication.SessionAuthentication',# 使用session时的认证器
# 'rest_framework.authentication.BasicAuthentication'# 提交表单时的认证器
# 'rest_framework_jwt.authentication.JSONWebTokenAuthentication',# 在DRF中配置JWT认证
],
'DEFAULT_PERMISSION_CLASSES': [
# 'rest_framework.permissions.IsAdminUser', # 管理员可以访问
# 'rest_framework.permissions.IsAuthenticated', # 认证用户可以访问
# 'rest_framework.permissions.IsAuthenticatedOrReadOnly', # 认证用户可以访问, 否则只能读取
# 'rest_framework.permissions.AllowAny', # 所有用户都可以访问
],
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
'user': '1000/hour', # 认证用户每小时100次
'anon': '300/day', # 未认证用户每天能访问3次
},
'DEFAULT_CONTENT_NEGOTIATION_CLASS': 'rest_framework.negotiation.DefaultContentNegotiation',
'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata',
'DEFAULT_VERSIONING_CLASS': None,
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10, # 默认 None
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
# 'django_filters.rest_framework.backends.DjangoFilterBackend', 包路径有变化
],
'SEARCH_PARAM': 'search',
'ORDERING_PARAM': 'ordering',
'NUM_PROXIES': None,
'DEFAULT_VERSION': None,
'ALLOWED_VERSIONS': None,
'VERSION_PARAM': 'version',
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
'UNAUTHENTICATED_TOKEN': None,
'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
'VIEW_DESCRIPTION_FUNCTION': 'rest_framework.views.get_view_description',
'NON_FIELD_ERRORS_KEY': 'non_field_errors',
'TEST_REQUEST_RENDERER_CLASSES': [
'rest_framework.renderers.MultiPartRenderer',
'rest_framework.renderers.JSONRenderer'
],
'TEST_REQUEST_DEFAULT_FORMAT': 'multipart',
'URL_FORMAT_OVERRIDE': 'format',
'FORMAT_SUFFIX_KWARG': 'format',
'URL_FIELD_NAME': 'url',
'UNICODE_JSON': True,
'COMPACT_JSON': True,
'STRICT_JSON': True,
'COERCE_DECIMAL_TO_STRING': True,
'UPLOADED_FILES_USE_URL': True,
'HTML_SELECT_CUTOFF': 1000,
'HTML_SELECT_CUTOFF_TEXT': "More than {count} items…",
'SCHEMA_COERCE_PATH_PK': True,
'SCHEMA_COERCE_METHOD_NAMES': {
'retrieve': 'read',
'destroy': 'delete'
},
}
案例中的相关配置
1.创建user/serializer.py 文件 用来写序列化
# -*- coding: utf-8 -*-
from rest_framework import serializers
from user.models import User
def address_validate(data):
return data
class UserSerializer(serializers.ModelSerializer):
address=serializers.CharField(max_length=255,min_length=5,validators=[address_validate])
def validate_address(self,data):
if data == "测试":
raise serializers.ValidationError('请填写实际地址')
return data
def validate_phone(self,data):
model=self.root.Meta.model
num=User.objects.filter(phone=data).count()
if num>0:
raise serializers.ValidationError('手机号')
return data
def validate(self,attrs):
return attrs
class Meta:
model=User
fields='__all__'
# fields=['username','address']
# exclude=['password']
read_only_fields=('',)
extra_kwargs={
"address":{
"min_length":5,
"default":"默认测试地址"
}
}
class UserInfoSerializer(serializers.Serializer):
id = serializers.CharField(read_only=True)# 普通字段,设置id为只读字段,不能修改
username = serializers.CharField(min_length=3,max_length=20,error_messages= {'required': '该字段必填'})# 显示普通字段
img = serializers.ImageField(required=False)
nick_name = serializers.CharField(max_length=20)
address = serializers.CharField(max_length=255)
xxx = serializers.SerializerMethodField(read_only=True)# 自定义显示(显示多对
class Meta:
model=User
def get_xxx(self,row):
users=row.username
return users
def create(self, validated_data):
return User.objects.create(**validated_data)
def update(self,instance,validated_data):
if validated_data.get('username'):
instance.username = validated_data['username']
instance.save()
return instance
def validate(self,attrs):
print(attrs)
if attrs.get("ussername")=='admin':
raise serializers.ValidationError('不能创建admin用户')
return attrs
class UserUnActiveSerializer(serializers.ModelSerializer):
class Meta:
model=User
fields=('id','username','is_active') #临时添加字段也需要写在这里
#fields="__all__" 所有字段
2.user/view.py 视图编写
from django.http import HttpResponse
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.permissions import AllowAny, IsAdminUser, IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from rest_framework.permissions import BasePermission, SAFE_METHODS
from .models import User
from .serializers import UserSerializer, UserUnActiveSerializer
# import markdown
def index(request):
#需要认证才能访问的视图
return HttpResponse('hello')
# 分页(局部):自定义分页器 局部
class PageNum(PageNumberPagination):
# 查询字符串中代表每页返回数据数量的参数名, 默认值: None
page_size_query_param = 'page_size'
# 查询字符串中代表页码的参数名, 有默认值: page
# page_query_param = 'page'
# 一页中多的结果条数
max_page_size = 2
# 自定义权限(局部)
class MyPermission(BasePermission):
# has_permission 是用户对这个视图有没有 GET POST PUT PATCH DELETE 权限的分别判断
def has_permission(self, request, view):
print('has_perm')
# print(view.kwargs.get("pk"), request.user.id)
# """判断用户对模型有没有访问权"""
# 任何用户对使用此权限类的视图都有访问权限
if request.user.is_superuser:
# 管理员对用户模型有访问权
return True
elif view.kwargs.get('pk') == str(request.user.id):
# 携带的id和用户的id相同时有访问权
return True
return False
# has_object_permission 是用户过了 has_permission 判断有权限以后,再判断这个用户有 没有对一个具体的对象有没有操作权限
# 这样设置以后,即使是django admin管理员也只能查询自己user标的信息,不能查询其他用户的 单条信息
def has_object_permission(self, request, view, obj):
print('has_object_perm')
# """获取单个数据时,判断用户对某个数据对象是否有访问权限"""
if request.user.id == obj.id:
return True
return False
class UserViewSet(viewsets.ModelViewSet):
'''
完成产品的增删改查
'''
queryset = User.objects.all()
serializer_class = UserSerializer # 优先使用 get_serializer_class 返回的序列化器
# 1.认证: 自定义认证类, 自定义会覆盖全局配置
authentication_classes = (BasicAuthentication, SessionAuthentication) #
# 2.权限: 自定义权限类
permission_classes = (MyPermission,)
# 3.分页: 自定义分页器 覆盖全局配置
pagination_class = PageNum
# 4.限流: 自定义限流类
throttle_classes = [UserRateThrottle]
# 5.过滤: 指定过滤方法类, 排序方法类, 一个或多个
filter_backends = (DjangoFilterBackend, OrderingFilter) # 同时支持过滤和排序
# 5.1指定排序字段, 不设置, 排序功能不起效
ordering_fields = ('date_joined', 'id') # ?ordering=-id
# 5.2指定过滤字段, 不设置, 过滤功能不起效
filter_fields = ('username', 'phone', 'is_active') # ? username=tom&phone=&is_active=true
# 根据不同的请求, 获得不同的序列化器
def get_serializer_class(self):
if self.action == 'unactived':
return UserUnActiveSerializer
else:
return UserSerializer
@action(methods=['get'], detail=False)
def unactived(self, request, *args, **kwargs):
# 获取查询集, 过滤出未激活的用户
qs = self.queryset.filter(is_active=False)
# 使用序列化器, 序列化查询集, 并且是
ser = self.get_serializer(qs, many=True)
return Response(ser.data)
@action(methods=['get'], detail=False)
def actived(self, request, *args, **kwargs):
# 获取查询集, 过滤出未激活的用户
qs = self.queryset.filter(is_active=True)
# 使用序列化器, 序列化查询集, 并且是
ser = self.get_serializer(qs, many=True)
return Response(ser.data)
3.user/urls.py 路由
from django.urls import path,include
from user import views
from rest_framework.routers import SimpleRouter,DefaultRouter
from rest_framework.authtoken.views import obtain_auth_token
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
router=DefaultRouter()
router.register(r'user',views.UserViewSet)
urlpatterns = [
path('index/',views.index),
path('api-auth/',include('rest_framework.urls',namespace='rest_framework')),
]
urlpatterns += router.urls # 模块地址
4.查询测试接口
手机扫一扫
移动阅读更方便
你可能感兴趣的文章