view demo

class ValidateCodeSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
serializer_class = VerifyCodeSerializer def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
mobile = serializer.validated_data["mobile"] # 验证成功可以通过validated_data获取值 code = generator_code()
print(len(code))
print(code)
verify_code = VerifyCode(mobile=mobile, code=code)
verify_code.save()
return Response({"mobile": mobile}, status=status.HTTP_200_OK)

serializerDemo

class VerifyCodeSerializer(serializers.Serializer):
mobile = serializers.CharField() def validate_mobile(self, mobile):
if User.objects.filter(mobile=mobile).count():
raise serializers.ValidationError("用户已存在") if not re.match(r"^1[35678]\d{9}$", mobile):
raise serializers.ValidationError("手机号非法") now = datetime.datetime.now()
onmin_ago = now - datetime.timedelta(hours=0, minutes=1, seconds=0)
if VerifyCode.objects.filter(add_time__gt=onmin_ago):
raise serializers.ValidationError("发送频率太高") return mobile

django drf CreateModelMixin和Serializer.validate_columun的更多相关文章

  1. django DRF理解

    django restframework(DRF) 最近的开发过程当中,发现restframework的功能很强大,所以尝试解读了一下源码,写篇博客分享给大家,有错误的地方还请各位多多指出 视图部分 ...

  2. 解决Django + DRF:403 FORBIDDEN:CSRF令牌丢失或不正确,{"detail":"CSRF Failed: CSRF cookie not set."}

    我有一个Android客户端应用程序尝试使用Django + DRF后端进行身份验证.但是,当我尝试登录时,我收到以下响应: 403: CSRF Failed: CSRF token missing ...

  3. Django DRF 分页

    Django DRF 分页 分页在DRF当中可以一共有三种,可以通过setttings设置,也可也通过自定义设置 PageNumberPagination 使用URL http://127.0.0.1 ...

  4. django drf 初探serializer

    1.定义Model对应的serializer from rest_framework import serializers class GoodsSerializer(serializers.Seri ...

  5. django drf unique_together和UniqueTogetherValidator

    联合唯一可以使用django中的unique_together,和DRF中的UniqueTogetherValidator->https://www.django-rest-framework. ...

  6. [django]drf知识点梳理-搜索

    什么是搜索? 譬如http://127.0.0.1:8000/User/?username=maotai-0 可以检索出想要的. 自己实现原始的搜索 重写下get_queryset方法 class U ...

  7. django drf JWT

    建议使用djangorestframework-jwt或者djangorestframework_simplejwt,文档为 https://github.com/GetBlimp/django-re ...

  8. django drf 级联数据和RetrieveModelMixin

    1.定义View from django.shortcuts import render from rest_framework.views import APIView from rest_fram ...

  9. django drf GenericAPIView和ListAPIView

    drf提供了更快捷的查询方法ListModelMixin+GenericAPIView,和ListAPIView 1.ListModelMixin+GenericAPIView from django ...

随机推荐

  1. MPI 集合通信函数 MPI_Scatterv(),MPI_Gatherv(),MPI_Allgatherv(),MPI_Alltoall(),MPI_Alltoallv(),MPI_Alltoallw()

    ▶ 函数 MPI_Scatterv() 和 MPI_Gatherv() .注意到函数 MPI_Scatter() 和 MPI_Gather() 只能向每个进程发送或接受相同个数的元素,如果希望各进程获 ...

  2. xml 注释中不允许出现字符串“--“(再也不要来坑爹了,好么,XML)

    转自:https://blog.csdn.net/randomnet/article/details/18708575?utm_source=blogxgwz3 关于xml文件时出现中文注释出错的一个 ...

  3. java String标准格式转换Date(yyyy-MM-dd HH:mm:ss)

    SimpleDateFormat sdf=new SimpleDateFormat("yyy年MM月dd日hh点mm分"); Date date=new Date(); Strin ...

  4. 小学生作业V2.0

    211606320刘佳&211506332熊哲琛 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Plann ...

  5. 59. Spiral Matrix II (Array)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. java文档打包成压缩包并且下载

    需求,根据产品ID查询产品详情,产品详情会返回产品的一些文案,以及图片的url.需要做成,将文案信息记录在一个txt文档中,然后图片下载到文件夹,最后下载到本地,下载后自动删除刚才生成的文件夹以及文件 ...

  7. 解题报告-683. K Empty Slots

    There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...

  8. golang之数组

    1.数组:同一种数据类型的固定长度的序列. 2.数组定义:var a [len]int,例如:var a [5]int 3.长度是数组类型的一部分,因此,var a[5] int 和 var a[10 ...

  9. Greeplum 系列(七) 权限管理

    Greeplum 系列(七) 权限管理 一.角色管理 Role 分为用户(User)和组(Group),用户有 login 权限,组用来管理用户,一般不会有 login 权限.初始化 gp 时创建了一 ...

  10. 命令: go build

    命令: go build 参考: https://studygolang.com/articles/9463 go help build 构建编译由导入路径命名的包,以及它们的依赖关系,但它不会安装结 ...