python day52

python day52

django测试环境搭建

方式有两种

方式一:任意创建一个py文件,在该文件内书写固定的配置

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

方式二:直接使用pycharm提供的python console

单表查询关键字

QuerySet对象方法

	query  # 查看orm内部对应的SQL语句
res = models.Books.objects.create(title='西游记',price=687.90)# create返回值就是当前被创建的数据对象
 print(res.title)
删 (一般实际不用这种,而是采用一个is_delete关键字)
models.Books.objects.filter(pk=1).delete()

改也有两种方式
方式一:

res = models.Books.objects.filter(pk=1).update(price=666.66)
print(res)  # 返回值是受影响的行数

方式二:

book_obj = models.Books.objects.filter(pk=2).first()# pk能够自动查找到当前表的主键字段 我们不需要查看当前表主键字段名
book_obj.price = 99.99
book_obj.save()  # 效率低(所有字段重新写一遍)

查也有两种方式
方式一:推荐使用

res = models.Books.objects.all()
 print(res.query)
res = models.Books.objects.filter()
print(res.query)
res = models.Books.objects.filter(title='jason')#filter括号内可以放多个参数 默认是and关系  推荐使用  条件不符合不会报错
 print(res)

方式二:

res1 = models.Books.objects.get(title='jason')#get括号内可以放多个参数 默认是and关系  不推荐使用  条件不符合直接报错
print(res1)
first()

取第一个数据对象

res = models.Books.objects.all().first()
res1 = models.Books.objects.all()[0]
res2 = models.Books.objects.all()[0:2]#QuerySet对象还支持切片操作 但是只能填正数
print(res,res1,res2)
last()

取最后一个数据对象

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

获取数据指定字段的值

res = models.Books.objects.all().values('title','publish_time')#all()加不加都表示所有数据  values获取的结果 类似于列表套字典"
res1 = models.Books.objects.values('title','publish_time')
print(res,res1)
values_list()

获取数据指定字段的值

res = models.Books.objects.values_list('title', 'publish_time')#values_list获取的结果 类似于列表套元组
print(res)
# 5.order_by()

排序

res = models.Books.objects.order_by('price')  # 默认是升序
print(res)
res1 = models.Books.objects.order_by('-price')  # 减号降序
print(res1)
count()

计数

res = models.Books.objects.count()  # 统计数据条数
print(res)
distinct()

去重

res = models.Books.objects.all().distinct()
res1 = models.Books.objects.values('title').distinct() #去重的前提是数据必须是一模一样  一定不能忽略主键
print(res1)
exclude()

排除什么什么在外 取反操作

res = models.Books.objects.exclude(title='西游记')
print(res.query)
reverse()
res = models.Books.objects.all() res1 = models.Books.objects.reverse()
res2 = models.Books.objects.order_by('price').reverse()#reverse需要先排序之后才能反转
print(res)
print(res1)
print(res2)
exists()

判断是否有数据 返回布尔值

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

双下划綫查询(范围查询)

1.查询价格大于700的书籍

res = models.Books.objects.filter(price__gt=700)
print(res)
print(res.query)

2.查询价格小于700的书籍

res = models.Books.objects.filter(price__lt=700)
print(res)
print(res.query)

3.查询价格要么是66.66 要么是99.99要么是10000

res = models.Books.objects.filter(price__in=[66.66,99.99,10000])#python对数字不是很敏感 精确度不高  很多时候我们会采取字符串存储数字类型
print(res)

4.查询价格在500到800之间的

res = models.Books.objects.filter(price__range=(500,800))
print(res.query)

5.查询书名中包含字母s的书

res = models.Books.objects.filter(title__contains='s')  # 区分大小写
print(res.query)
res = models.Books.objects.filter(title__icontains='s')  # 区分大小写
print(res)

6.查询出版日期是2021的书

res = models.Books.objects.filter(publish_time__year=2021)
print(res.query)

7.查询出版日期是3月的书

res = models.Books.objects.filter(publish_time__month=3)
print(res)
图书管理系统表设计
class Book(models.Model):
    title = models.CharField(verbose_name='书名', max_length=32)
    price = models.DecimalField(verbose_name='价格', max_digits=8, decimal_places=2)
    publish_time = models.DateField(verbose_name='出版日期',auto_now_add=True)
    # 一对多 外键字段建在多的一方
    publish = models.ForeignKey(to='Publish')
    # 多对多 外键字段推荐建在查询频率较高的表中
    authors = models.ManyToManyField(to='Author')


class Publish(models.Model):
    title = models.CharField(verbose_name='名称',max_length=32)
    addr = models.CharField(verbose_name='地址',max_length=128)
    email = models.EmailField(verbose_name='邮箱')


class Author(models.Model):
    name = models.CharField(verbose_name='姓名',max_length=32)
    age = models.IntegerField(verbose_name='年龄')
    # 一对一 外键字段推荐建在查询频率较高的表中
    author_detail = models.OneToOneField(to='AuthorDetail')


class AuthorDetail(models.Model):
    phone = models.BigIntegerField(verbose_name='电话')
    addr = models.CharField(verbose_name='地址',max_length=32)

跨表查询理论

正向:外键字段在谁那儿,谁查另外的人就是正向

反向:没有外键字段

子查询

1.先查询出一个对象
2.基于对象点正反向字段

反向查询表明小写加_set表示查询的对象可能有多个的情况。在查询的对象只有一个的情况不需要加。

基于对象的跨表查询

1.查询聊斋书籍对应的出版社名称

book_obj = models.Book.objects.filter(title='聊斋').first()
res = book_obj.publish
print(res.title)

2.查询神雕侠侣对应的作者

    book_obj = models.Book.objects.filter(title='神雕侠侣').first()
    # res = book_obj.authors  # app01.Author.None
    res = book_obj.authors.all()
    print(res)

3.查询jason的地址

author_obj = models.Author.objects.filter(name='jason').first()
res = author_obj.author_detail
print(res.phone,res.addr)

4.查询东方出版社出版过的书籍

publish_obj = models.Publish.objects.filter(title='东方出版社').first()
res = publish_obj.book_set  # app01.Book.None
res = publish_obj.book_set.all()  # app01.Book.None
print(res)

5.查询zhangsan写过的书

author_obj = models.Author.objects.filter(name='zhangsan').first()
res = author_obj.book_set  # app01.Book.None
res = author_obj.book_set.all()
print(res)

6.查询电话是110的作者姓名

author_detail_obj = models.AuthorDetail.objects.filter(phone=110).first()
res = author_detail_obj.author
print(res.name,res.age)
连表操作

基于对象的跨表查询

1.查询聊斋书籍对应的出版社名称

book_obj = models.Book.objects.filter(title='聊斋').first()
res = book_obj.publish
print(res.title)

2.查询神雕侠侣对应的作者

book_obj = models.Book.objects.filter(title='神雕侠侣').first()
res = book_obj.authors.all()
print(res)

3.查询jason的地址

author_obj = models.Author.objects.filter(name='jason').first()
res = author_obj.author_detail
print(res.phone,res.addr)

4.查询东方出版社出版过的书籍

# publish_obj = models.Publish.objects.filter(title='东方出版社').first()
# # res = publish_obj.book_set  # app01.Book.None
# res = publish_obj.book_set.all()  # app01.Book.None
# print(res)

5.查询zhnagsan写过的书
author_obj = models.Author.objects.filter(name=‘zhnagsan’).first()
res = author_obj.book_set.all()
print(res)

6.查询电话是110的作者姓名

author_detail_obj = models.AuthorDetail.objects.filter(phone=110).first()
res = author_detail_obj.author
print(res.name,res.age)
基于双下划线查询
正向

1.查询聊斋书籍对应的出版社名称

res = models.Book.objects.filter(title='聊斋').values('publish__title')
print(res.query)

2.查询神雕侠侣对应的作者名字和年龄

res = models.Book.objects.filter(title='神雕侠侣').values('authors__name','authors__age')
print(res)

3.查询jason的地址

res = models.Author.objects.filter(name='jason').values('author_detail__addr')
print(res)
反向

1.查询聊斋书籍对应的出版社名称

res = models.Publish.objects.filter(book__title='聊斋').values('title')
print(res)

2.查询神雕侠侣对应的作者名字和年龄

res = models.Author.objects.filter(book__title='神雕侠侣').values('name','age')
print(res)

3.查询jason的地址

res = models.AuthorDetail.objects.filter(author__name='jason').values('addr')
print(res)

查询神雕侠侣对应的作者的电话和地址

res = models.Book.objects.filter(title='神雕侠侣').values('authors__author_detail__phone','authors__author_detail__addr')
print(res)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值