六、模型层

一、配置测试脚本

1. 在某一个应用的test.py中加上下面代码,可在manage.py中复制一部分。

    import os
    
    
    if __name__ == "__main__":
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django1.settings")
    
        import django
        django.setup()


2. 新建任意py文件,写入上面代码。

二、queryset对象

1. 能用queryset方法链式操作
    
        res = models.Books.objects.filter(pk=1).filter()
    
2. 能用‘.queryset’查看结果内部对应的sql语句

三、单表操作之增、删、改

1. 创建数据

    a. create方法
    
        from datetime import date
        
        ctime = date.today()
        book_obj = models.Books.objects.create(title='红楼梦', publish_date=ctime)
    
        book_obj = models.Books.objects.create(title='三国演义',publish_date='2019-1-1')
    
    
    b. 利用对象的绑定方法
    
        book_obj = models.Books(title='西游记', price=200.00, publish_date='2000-1-2')
        book_obj.save()
        
2. 修改数据

    a. queryset方法
    
        models.Books.objects.filter(pk=1).update(price=111.00)
    
    b. 利用对象
        
        将数据所有字段重写一遍,不推荐使用
        
        book_obj = models.Books.objects.get(pk=1)
        book_obj.price = 222.00
        book_obj.save()

3. 删除数据

    a. queryset方法
    
        models.Books.objects.filter(pk=3).delete()
    
    b. save方法
    
        book_obj = models.Books.objects.get(pk=1)
        book_obj.delete()
        

四、单表查询数据的13条方法。

orm语句的查询默认都是惰性查询,只有在使用数据时才会执行。

1. all() 

    查询所有,返回queryset对象。

    res = models.Books.objects.all()
    print(res)

2. filter()

    筛选,相当于原生sql语句里的where。支持多个参数,参数之间and关系。返回queryset对象。
    
    res = models.Books.objects.filter(pk=1, title='三国演义')
    print(res)

3. get()

    筛选,获取对象本身,条件不存在报错,支持一个参数。

    res = models.Books.objects.get(pk=1)
    print(res)

4. first() 

    获取queryset中第一个数据对象
    
    res = models.Books.objects.filter(title='西游记').first()
    print(res.price)


5. last() 

    获取queryset中最后一个数据对象
    
    res = models.Books.objects.filter(title='西游记').last()
    print(res.price)


6. count()

    统计数据个数
    
    num = models.Books.objects.count()
    print(num)

7. values()

    获取数据对象中指定的字段值,返回queryset对象(列表套字典)
    
    res = models.Books.objects.values('title', 'price')
    print(res)

8. values_list()

    获取数据对象中指定的字段值,返回queryset对象(列表套元祖)

    res = models.Books.objects.values_list('title', 'price')
    print(res)
    
9. order_by()

    按照指定字段排序,默认升序,字段前加负号降序

    res1 = models.Books.objects.order_by('price')
    res2 = models.Books.objects.order_by('-price')
    print(res1, res2)
    

10. reverse()

    颠倒顺序,前提是对象已被排序
    
    res = models.Books.objects.order_by('price').reverse()
    print(res)

11. exclude()

    排除
    
    res = models.Books.objects.exclude(pk=2)
    print(res)
    
12. exist()

    判断查询结果是否有值,返回布尔值,没什么用,数据本身自带布尔值。

    res = models.Books.objects.filter(pk=2).exists()
    print(res)
    
13. distinct()

    去重,容易忽略主键。

    res = models.Books.objects.values('title', 'price').distinct()
    print(res)
    

五、单表查询的双下划线查询

1. __gt/__lt

    查询大于/小于某一个值的数据

    res = models.Books.objects.filter(price__gt=100)
    print(res)
    
2. __gte/__lte
    
    查询大于等于/小于等于某一个值的数据
    
    res = models.Books.objects.filter(price__lte=100)
    print(res)

3. __in

    查询价格等于100或200的数据

    res = models.Books.objects.filter(price__in=[100, 200])
    print(res)

4. __range

    查询价格在100到200之间的数据,顾头顾尾。

    res = models.Books.objects.filter(price__range=(100, 200))
    print(res)

5. __year

    查询出版年份是2019年的数据

    res = models.Books.objects.filter(publish_date__year='2019')
    print(res)

6. __month

    查询出版月份是1月的数据

    res = models.Books.objects.filter(publish_date__month='1')
    print(res)

7. __startswith/__endswith

    查询名称以‘三’开头/结尾的数据

    res = models.Books.objects.filter(title__startswith='三')
    print(res)

8. __contains/__icontains

    查询名称中包含‘a’字的数据,不忽略/忽略大小写。

    res = models.Books.objects.filter(title__contains='a')
    print(res)

猜你喜欢

转载自www.cnblogs.com/binyuanxiang/p/12052216.html