Django--模型层

13个方法

  all()

  filter()

  get()

  reverse()  

  order_by()

  exclude()

  values()

  values_list()

  count()

  distinct()

  exists()

  first()

  last()

 

神奇的双下滑操作

  查看orm内部sql语句的方法有哪些

    1、如果是queryset对象,那么可以点query直接查看该queryset的内部的sql语句

    2、在Django项目的配置文件中,配置一下参数就可以实现所有的orm在查询的时候自动打印对应的sql语句

LOGGING = {
                    'version': 1,
                    'disable_existing_loggers': False,
                    'handlers': {
                        'console':{
                            'level':'DEBUG',
                            'class':'logging.StreamHandler',
                        },
                    },
                    'loggers': {
                        'django.db.backends': {
                            'handlers': ['console'],
                            'propagate': True,
                            'level':'DEBUG',
                        },
                    }
                }

 

    

# 查询价格大于200的书籍
        # res = models.Book.objects.filter(price__gt=200)
        # print(res)
        # 查询价格小于200的书籍
        # res = models.Book.objects.filter(price__lt=200)
        # print(res)

        # 查询价格大于等于200.22的书籍
        # res = models.Book.objects.filter(price__gte=200.22)
        # print(res)
        # 查询价格小于等于200.22的书籍
        # res = models.Book.objects.filter(price__lte=200.22)
        # print(res)


        # 查询价格要么是200,要么是300,要么是666.66
        # res = models.Book.objects.filter(price__in=[200,300,666.66])
        # print(res)
        # 查询价格在200到800之间的
        # res = models.Book.objects.filter(price__range=(200,800))  # 两边都包含
        # print(res)

        # 查询书籍名字中包含p的
        """原生sql语句 模糊匹配
            like 
                %
                _
        """
        # res = models.Book.objects.filter(title__contains='p')  # 仅仅只能拿小写p
        # 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(res)
        # print(res1)
            
        
        # 查询出版日期是2017的年(******)
        res = models.Book.objects.filter(create_time__year='2017')
        print(res)

 

 

 

  多表操作

    一对多(foreignKey)

    一对一(OneToOneField)  可以用foreignKey来代替)ForeignKey(unique=True)

      一对多和一对一两个关键字创建出来的字段会自动加上_id后缀

    多对多(MangToManyField)

      该字段并不会真正的在表中展示出来,它仅仅只是一个虚拟字段

      1、告诉orm自动创建第三张表

      2、帮助orm跨表查询

    

    一对多字段的跨表查询

    多对多字段的跨表查询

    orm跨表查询

      基于对象的跨表查询

      基于双下划线的查询

    聚合查询

      aggtegate

      from django.db.models import Sum, Max, Min, Avg, Count

 

    分组

      annotate

    F与Q查询

      F查询

      Q查询

            q = Q()
                    q.connector = 'or'  # 修改查询条件的关系   默认是and
                    q.children.append(('title__contains','三国演义'))  # 往列表中添加筛选条件
                    q.children.append(('price__gt',444))  # 往列表中添加筛选条件
                    res = models.Book.objects.filter(q)  # filter支持你直接传q对象  但是默认还是and关系
                    print(res)

 

      ORM常见字段

      ORM事物操作

      ORM查询优化  

    

 

 

 

 

 

 

 

 

 

 

 

    

posted @ 2019-09-20 08:49  tulintao  阅读(151)  评论(0编辑  收藏  举报