美文网首页
Django学习-第八讲:django ORM数据库查询,修改等

Django学习-第八讲:django ORM数据库查询,修改等

作者: 小海怪的互联网 | 来源:发表于2019-10-02 13:39 被阅读0次

    1.查询操作

    查找是数据库操作中一个非常重要的技术。查询一般就是使用filterexclude以及get三个方法来实现。我们可以在调用这些方法的时候传递不同的参数来实现查询需求。在ORM层面,这些查询条件都是使用field+__+condition的方式来使用的。

    1.1.查询条件

    • 1 .exact

    使用精确的=进行查找。如果提供的是一个None,那么在SQL层面就是被解释为NULL

    article = Article.objects.get(id__exact=14)
    article = Article.objects.get(id__exact=None)
    
    以上的两个查找在翻译为SQL语句
    
    select * from article where id=14;
    select * from article where id IS NULL;
    
    

    article.query,可以得到Django执行的SQL语句。但是只能作用于QuerySet对象上。

      1. iexact

    使用like进行查找。

    article = Article.objects.filter(title__iexact='hello world')
    
    那么以上的查询就等价于以下的SQL语句:
    
    select * from article where title like 'hello world';
    

    注意上面这个sql语句,因为在MySQL中,没有一个叫做ilike的。所以exact和iexact的区别实际上就是LIKE和=的区别,在大部分collation=utf8_general_ci情况下都是一样的(collation是用来对字符串比较的)。

      1. contains

    大小写敏感,判断某个字段是否包含了某个数据。

    articles = Article.objects.filter(title__contains='hello')
    
    在翻译成SQL语句为如下:
    
    select * where title like binary '%hello%';
    

    要注意的是,在使用contains的时候,翻译成的sql语句左右两边是有百分号的,意味着使用的是模糊查询。而exact翻译成sql语句左右两边是没有百分号的,意味着使用的是精确的查询。

    • 4.icontains

    大小写不敏感的匹配查询。

    articles = Article.objects.filter(title__icontains='hello')
    在翻译成SQL语句为如下:
    
    select * where title like '%hello%';\
    
      1. in
        提取那些给定的field的值是否在给定的容器中。容器可以为list、tuple或者任何一个可以迭代的对象,包括QuerySet对象。
    articles = Article.objects.filter(id__in=[1,2,3])
    以上代码在翻译成SQL语句为如下:
    
    select *from articles where id in (1,2,3)
    
    当然也可以传递一个QuerySet对象进去。
    # 查找标题为hello的文章分类
    articles = Article.objects.filter(title__icontains="hello")
    category = Category.objects.filter(article__in=articles)
    
    # 查找文章ID为1,2,3的文章分类
    category = Category.objects.filter(article__id__in=[1,2,3])
    
      1. 根据关联的表进行查询
        想要获取文章标题中包含"hello"的所有的分类。那么可以通过以下代码来实现:
    categories = Category.object.filter(article__title__contains("hello"))
    

    2. 比较运算

    1. gt

    某个field的值要大于给定的值。
    # 将所有id大于4的文章全部都找出来。
    articles = Article.objects.filter(id__gt=4)
    将翻译成以下SQL语句:
    select * from articles where id > 4;
    
    

    2. gte

    类似于gt,是大于等于。

    3.lt

    类似于gt是小于。

    4.lte

    类似于lt,是小于等于。

    5. range

    判断某个field的值是否在给定的区间中。

    start_date = datetime(year=2019, month=12, day=1,hour=10,minute=0,second=0)
    end_date = datetime(year=2019, month=12, day=30,hour=10,minute=0,second=0)
    date_range = Common.objects.filter(test_date__range=(start_date,end_date))
    
    以上代码的意思是提取所有发布时间在2019/1/1到2019/12/12之间的文章。
    将翻译成以下的SQL语句:
    SELECT `user_common`.`id`, `user_common`.`content`, `user_common`.`pid`, `user_common`.`test_date` FROM `user_common` WHERE `user_common`.`test_date` BETWEEN 2019-12-01 02:00:00 AND 2019-12-30 02:00:00。
    
    

    6. date

    针对某些date或者datetime类型的字段。可以指定date的范围。并且这个时间过滤,还可以使用链式调用。

    date_test = Common.objects.filter(test_date__date=datetime(year=2018,month=12,day=19))
    
    print(date_test.query)
    print(date_test)
    
    翻译成SQL语句:
    SELECT `user_common`.`id`, `user_common`.`content`, `user_common`.`pid`, `user_common`.`test_date` FROM `user_common` WHERE DATE(`user_common`.`test_date`) = 2018-12-19
    
    

    7. year

    根据年份进行查找。

    articles = Article.objects.filter(pub_date__year=2018)
    articles = Article.objects.filter(pub_date__year__gte=2017)
    以上的代码在翻译成SQL语句为如下:
    
    select ... where pub_date between '2018-01-01' and '2018-12-31';
    select ... where pub_date >= '2017-01-01';
    
    

    8.time

    根据时间进行查找。示例代码如下:

    articles = Article.objects.filter(pub_date__time=time(hour=15,minute=21,second=10))
    以上的代码是获取每一天中15点21分10秒发表的所有文章。
    
    # 查询10秒到11秒之间的
    start_date = time(hour=17,minute=21,second=10)
    end_date = time(hour=17,minute=21,second=11)
    date_test = Common.objects.filter(test_date__time__range = (start_date,end_date))
    

    更多内容可以参照官文档

    相关文章

      网友评论

          本文标题:Django学习-第八讲:django ORM数据库查询,修改等

          本文链接:https://www.haomeiwen.com/subject/arolpctx.html