zoukankan      html  css  js  c++  java
  • django filter判断是否存在? 效率问题

    result= Booking.objects.filter(xxxx,xxxx,xxxx)
    count :获取提取的数据的个数。如果想要知道总共有多少条数据,那么建议使用 count ,而不是使用 len(articles) 这种。因为 count 在底层是使用 select count(*) 来实现的,这种方式比使用 len 函数更加的高效;

      first 和 last :返回 QuerySet 中的第一条和最后一条数据,如果原来数据集就是空,则返回Null;

      aggregate :使用聚合函数;

      exists :判断某个条件的数据是否存在。如果要判断某个条件的元素是否存在,那么建议使用 exists ,这比使用 count 或者直接判断QuerySet 更有效得多。

    #方法一 .exists()
    if result.exists():
        return Response({'已存在,请勿重复添加'},status=status.HTTP_400_BAD_REQUEST)
    else:
     
      添加操作。
    #方法二 .count()==0 
    if result.count() == 0:
      添加操作。
    #方法三 if result:
    return Response({'已存在,请勿重复添加'},status=status.HTTP_400_BAD_REQUEST) else: 添加操作

    总结: QuerySet.exists()
    > QuerySet.count()==0 > QuerySet
  • 相关阅读:
    KMP算法精髓
    习题
    JavaScript function函数种类介绍
    街景地图 API
    电脑网卡
    框架的设计之IRepository还是IRepository<T>
    顺序线性表
    hdu4284之字典树
    pt-table-checksum
    C++中输入输出流及文件流操作笔记
  • 原文地址:https://www.cnblogs.com/zxs117/p/12855924.html
Copyright © 2011-2022 走看看