zoukankan      html  css  js  c++  java
  • queryset和model

    查询语法:
    model对象.get
    queryset.all() or filter()
    链式操作
    queryset 数据类型 queryset类型 返回的是model对象
    在modles的class里添加下函数
    def __str__(self):
    return self.title
    实例化直接 显示名字
    select * 效率低
    加上__str__

    ****all() 查询所有
    select tile,price,.. from 表 ORM帮助做的
    book = Book.objects.all() objects管理器

    ****filter() 查询条件 可以多条件
    book = Book.objects.filter(title='python',price=100) 书名and价格
    objects管理器

    ****get方法 必须是一个 一般不敢用
    Book.objects.get(title='python')
    objects管理器
    filter是queryset get是直接拿对象 拿到结果超过一个或者0 直接报错 必须是一个
    返回model对象 model对象的特点 可以直接调用属性

    first() or last() 由queryset
    book = Book.objects.all()[0] 也是取第一个对象
    book = Book.objects.all().first() 取第一个对象
    book = Book.objects.all().last() 取最后一个对象
    返回值queryset

    exclude() not filter 不等于 排除 由objects管理器
    book = Book.objects.exclude(title='title')
    返回值queryset

    order_by() 排序 由queryset
    book = Book.objects.all().order_by('title')asc or ('-title')desc 可以多个条件
    返回值queryset
    book = Book.objects.all().order_by('title').first() 查到最大的值

    count() 个数 由queryset
    book = Book.objects.all().count() 总个数
    返回值 int类型

    reverse() 倒序 由queryset
    book = Book.objects.all().order_by('title').reverse()
    返回值queryset
    book = Book.objects.all().order_by('title').reverse().count()
    返回值 int类型

    exists() 是否有数据 由queryset
    空列表一定是 False
    select * from 表 limit 1 ==exists() 分页1本 有==true 无==false
    返回值 布尔类型

    关键: 重点 难点
    values() 比如我只要title 没必要把所有字段都拿出来 列表里面套字典[{'title':'书名'}]
    由queryset 用的最多
    book = Book.objects.all().values('title') 只拿title 可以多个参数
    select title from 表 可以提高性能 没必要把所有字段都拿出来
    返回值queryset

    values_list() [('书名',),('书名',),('书名',)]
    [('书名','价格'),('书名','价格'),('书名','价格')]
    由queryset
    book = Book.objects.all().values_list('title') 可以多个参数
    返回值queryset

    distinct() 去重 由queryset
    要在value或value_list
    book = Book.objects.all().value('title').distinct()
    包含主键 不可能去重
    要先用value('title')找到重复的 在.distinct()去重
    返回值queryset
              


    单表查询:
        查询API:
        queryset
        
        (1) all() :  调用者:objects管理器  返回queryset
        (2) filter() :调用者:objects管理器  返回queryset
        (3) get方法():调用者:objects管理器  返回查询到model对象 (注意:查询结果有且只有一个才执行)
        (4) first(),last()方法:调用者:queryset   返回model对象
        (5) exclude():调用者:objects管理器  返回queryset
        (6) order_by():由queryset对象调用,返回值是queryset
        (7) count :数数  :由queryset对象调用 返回int
        (8) reverse():由queryset对象调用,返回值是queryset
        (9) exists(): 由queryset对象调用 返回值布尔值
        (10)values()方法: 由queryset对象调用,返回值是queryset
        (11)values_list():由queryset对象调用,返回值是queryset
        (12)distinct(): 由queryset对象调用,返回值是queryset
        
        模糊查询(双下划线)
            Book.objects.filter(price__in=[100,200,300])
            Book.objects.filter(price__gt=100)
            Book.objects.filter(price__lt=100)
            Book.objects.filter(price__range=[100,200])
            Book.objects.filter(title__contains="python")
            Book.objects.filter(title__icontains="python")
            Book.objects.filter(title__startswith="py")
            Book.objects.filter(pub_date__year=2012)


  • 相关阅读:
    go相关
    mac下使用vscode技巧
    mac下secureCRT的使用技巧
    python subprocess实时输出
    python中多级目录导入模块问题
    python的print与sys.stdout
    python中类相关笔记
    python中sys.stdout.flush()的作用
    nginx+uwsgi配置
    虚拟机的 基本配置
  • 原文地址:https://www.cnblogs.com/xuexihainan/p/13436856.html
Copyright © 2011-2022 走看看