zoukankan      html  css  js  c++  java
  • model 补充一

    1:

    def annotate(self, *args, **kwargs)
        # 用于实现聚合group by查询

    2:

    def distinct(self, *field_names)
        # 用于distinct去重
        models.UserInfo.objects.values('nid').distinct()
        # select distinct nid from userinfo
    
        注:只有在PostgreSQL中才能使用distinct进行去重

    3:

     models.UserInfo.objects.all()
     models.UserInfo.objects.all().reverse()
    这样取得数据一样

     models.UserInfo.objects.all().order_by('-id')
     models.UserInfo.objects.all().order_by('-id').reverse()
    这样reverse才作用,上面是对id desc排序;而下面这对id正序排列


    4:
    from TB1
     Entry.objects.extra(select={'new_id': "select col from sometable where othercol > 1"}
    相当于SQL语句:

    select
      id,
      (select col from sometable where othercol > 1) as new_id
    from TB1

    5:
    def only(self, *fields):
        #仅取某个表中的数据
         models.UserInfo.objects.only('username','id')
         或
         models.UserInfo.objects.filter(...).only('username','id')

    only('username','id')===》[obj,obj,obj,]

    6:

    def defer(self, *fields):
        models.UserInfo.objects.defer('username','id')
        或
        models.UserInfo.objects.filter(...).defer('username','id')
        #映射中排除某列数据
     







  • 相关阅读:
    小菜菜mysql练习50题解析——数据准备
    C语言(数据结构)——概述
    运行 jar
    Hive 语句
    java14 IO流缓冲区 input output
    java 14 IO流
    java 14 图片的读取和写入
    java 集合的基础2
    java 13 hashmao的entryset()
    java 13 集合的基础
  • 原文地址:https://www.cnblogs.com/xyd134/p/6886022.html
Copyright © 2011-2022 走看看