django的ORM

ORM查询

    如果你向查看orm语句内部真正的sql语句有两种方式
        1.如果是queryset对象 可以直接点query查看
        2.配置文件中 直接配置
        LOGGING = {
            'version': 1,
            'disable_existing_loggers': False,
            'handlers': {
                'console': {
                    'level': 'DEBUG',
                    'class': 'logging.StreamHandler',
                },
            },
            'loggers': {
                'django.db.backends': {
                    'handlers': ['console'],
                    'propagate': True,
                    'level': 'DEBUG',
                },
            }}
    只要是queryset对象就可以无限制的点queryset对象的方法
        queryset.filter().filter().filter()

必会十三太保

查
    1.all()  查询所有           QuerySet
    res = models.Book.objects.all()  # 惰性查询
    print(res)i
    for i in res:
        print(i.title)

    2.filter()                  QuerySet
    res = models.Book.objects.filter(pk=2)
    print(res)

    3.get()                        数据对象本身

    4.first()     拿第一个
    res = models.Book.objects.all()
    print(res)
    print(res.first())
    
    5. last()     拿最后一个
    res = models.Book.objects.all()
    print(res)
    print(res.last())

    6.exclude  除此之外        QuerySet
    res = models.Book.objects.exclude(pk=3).filter(pk=4).filter(pk=1).filter(pk=4)
        # # <QuerySet []>
        # print(res)

    7.values  QuerySet    列表套字典
    res = models.Books.objects.values('title')
    for r in res:
        print(r.get('title'))


    8.values_list  QuerySet    列表套元组
    res = models.Book.objects.values_list('title')
    print(res)


    9.count()  统计数据的个数
    res = models.Book.objects.count()
    res1 = models.Book.objects.all().count()
    print(res,res1)

    10.distinct() 去重
     #去重:数据必须是一模一样的情况下才能去重
    res = models.Book.objects.all().distinct()
    res1 = models.Book.objects.values('title','price').distinct()
    print(res1)
    
    11.order_by()
    res = models.Book.objects.order_by('price')  # 默认是升序
    # res1 = models.Book.objects.order_by('-price')  # 加负号就是降序
    print(res)
    
    12.reverse()  前面必须是先结果排序才可以反转
    res = models.Book.objects.order_by('price').reverse()
    print(res)
    
    13.exists()  一点卵用没有
    res = models.Book.objects.filter(pk=1).exists()
    print(res)

神奇的双下划线

 查询价格大于200的书籍
    res = models.Book.objects.filter(price__gt=200)
    
    查询价格小于200的书籍
    res = models.Book.objects.filter(price__lt=200)
    
    print(res)
    查询价格大于或者等于200的书籍
    res = models.Book.objects.filter(price__gte=200)
    res1 = models.Book.objects.filter(price__lte=200)
    print(res,res1)

    价格是200 或者是123.23 或者666.66
    res = models.Book.objects.filter(price__in=[200,123.23,666.66])
    print(res)
    价格在200 到700之间的书籍
    
    res = models.Book.objects.filter(price__range=(200,666.66))  # 顾头不顾尾
    print(res)

    查询书籍名称中包含p的(模糊查询  like)
    res = models.Book.objects.filter(title__contains='p')  # 区分大小写
    print(res)
    忽略大小写
    res = models.Book.objects.filter(title__icontains='p')  # 忽略大小写
    print(res)


    查询书籍名称是以三开头的书籍
    res = models.Book.objects.filter(title__startswith='三')
    res1 = models.Book.objects.filter(title__endswith='P')
    print(res1)


    查询出版日期是2019年的书籍
    res = models.Book.objects.filter(publish_date__year='2019')
    查询出版日期是10月的书籍
    res = models.Book.objects.filter(publish_date__month='10')

多对多字段

                # 朝第三张关系表中添加数据
                book_obj.authors.add(1)
                book_obj.authors.add(1,2,3,4)
                book_obj.authors.add(author_obj)
                book_obj.authors.add(author_obj,author_obj1,author_obj2)
                # 朝第三张表修改数据
                book_obj.authors.set((1,))
                book_obj.authors.set((1,2,3))
                book_obj.authors.set((author_obj,))
                book_obj.authors.set((author_obj,author_obj1))
                # 朝第三张表删除关系
                book_obj.authors.remove(1)
                book_obj.authors.remove(1,2,3,4)
                book_obj.authors.remove(author_obj)
                book_obj.authors.remove(author_obj,author_obj1)
                # 朝第三张表清空当前书籍对象所有的记录
                book_obj.authors.clear() 
          
		跨表查询
            正反向的概念:
                外键字段在谁那儿 谁就是正向
                没有外键字段的  就是反向
                口诀:
                正向查询按字段
                反向查询按表名小写(结果多个要加_set)
原文地址:https://www.cnblogs.com/oxtime/p/11735306.html