博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python---django中orm的使用(2)
阅读量:4315 次
发布时间:2019-06-06

本文共 3459 字,大约阅读时间需要 11 分钟。

1.基于对象的正向查询和反向查询

在中也提到了正向和反向查找

表:一对多  书籍和出版社

class Book(models.Model):    title = models.CharField(max_length=100)    authors = models.ManyToManyField(Author)    publisher = models.ForeignKey(Publisher)    publication_date = models.DateField()    price=models.DecimalField(max_digits=5,decimal_places=2,default=10)    def __str__(self):        return self.title
Book(多)
class Publisher(models.Model):    name = models.CharField(max_length=30, verbose_name="名称")    address = models.CharField("地址", max_length=50)    city = models.CharField('城市',max_length=60)    state_province = models.CharField(max_length=30)    country = models.CharField(max_length=50)    website = models.URLField()     class Meta:        verbose_name = '出版商'        verbose_name_plural = verbose_name     def __str__(self):        return self.name
出版社(一)

正向查询:

from blog import modelsdef data_oper(req):        obj = models.Book.objects.filter(id=2)[0]        print(obj.publisher.name)        print(obj.publisher.city)        #正向查询,正常查找

反向查询:

from blog import modelsdef data_oper(req):        obj = models.Publisher.objects.filter(id=6)[0]        print(obj.book_set.all().values("title").distinct())   #_set是一个queryset集合        #反向查找,需要加上_set

上面正向反向都是基于对象的查询

2.双下划线(__)

双下划线(__)之单表条件查询
models.Book.objects.filter(id__lt=10,id_gt=1)获取id大于1,小于10的值   andmodels.Book.objects.filter(id__in=[11,12,13])获取id在11,12,13中的数据models.Book.objects.exclude(id__in=[11,12,13])    not inmodels.Book.objects.exclude(title__contains="ven")  #contains类似于like模糊查询models.Book.objects.exclude(title__icontains="ven")  #icontains大小写不敏感 models.Book.objects.exclude(title__startwith="p")
 
models.Book.objects.filter(id__range=[1,10])获取在1-10之间的数据......像: 字段名__isnull  判断字段是否为空

 

双下划线(__)之关联表条件查询

一对多,多对多没有区别

正向查询models.Book.objects.filter(title="fsa").values("publish__name")models.Book.objects.filter(publish__name="ffa出版社").values("title")反向查询models.Publish.objects.filter(book__title="XX书",book__id=5).values("name").distinct() ....

关联查询,表之间需要有约束,外键,或者ManyToManyField。不能随便将无关表关联

3.聚合查询 aggregate(*args,**kwargs)和分组查询annotate(*args,**kwargs)(了解)

都属于聚合函数

聚合查询是在查询结果集上进行操作,只有一个结果

分组查询是根据条件对结果进行分组,然后对应每个分组,来进行操作,有多个结果

from django.db.models import Avg,Min,Sum,Max  #导入聚合函数

聚合查询aggregate

models.Book.objects.all().aggregate(Avg("price"),Min("price"),Max("price"))

分组查询annotate:分组条件放在values中

models.Book.objects.values("authors_name").annotate(Sum("price"))#对每个作者分组

4.F和Q查询(重点)

需求引入:

将所有商品价格增加20

models.Book.objects.all().update(price="price"+20)#错误,update不能对某列进行运算,可以直接赋值解决方法,循环每个对象,单个修改,或者迭代,或者使用F

F查询:用来对某一列值进行操作

from django.db.models import Fdef data_oper(req):        models.Book.objects.all().update(price=F("price")+20)      models.Book.objects.all().update(price=F("title")+'tt')

Q查询:

前面filter()条件过滤,要么是单字段,或者and

Q:可以封装关键字查询,多关键字查询

from django.db.models import Qdef data_oper(req):        models.Book.objects.filter(id=3)[0]        print(obj)        #打印对象的地址(若无__str__)        models.Book.objects.filter(Q(id=3)|Q(title="php"))[0] #or        print(obj)                models.Book.objects.filter(Q(price_gt=50) & (Q(id=3)|Q(title="php")))[0] #and和or        print(obj)        
     models.Book.objects.filter(Q(price_gt=50) & (Q(id=3)|Q(title="php")),color="yellow")[0]
  可以把Q对象和关键字查询一起使用,但是一定要把Q对象放在关键字查询前面        结果是列表,包含所有结果

 或者可以使用:

q =Q()q.connector = "OR"q.children.append(('id',3))      #注意这里是元组q.children.append(('title','php')) models.Book.objects.filter(q)

 

转载于:https://www.cnblogs.com/ssyfj/p/8653765.html

你可能感兴趣的文章
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
WordPress资源站点推荐
查看>>
Python性能鸡汤
查看>>
android Manifest.xml选项
查看>>
Cookie/Session机制具体解释
查看>>
ATMEGA16 IOport相关汇总
查看>>
有意思的cmd命令
查看>>
js正則表達式语法
查看>>
Git学习系列-Git基本概念
查看>>
c#多个程序集使用app.config 的解决办法
查看>>
Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)
查看>>
Linux下获取本机IP地址的代码
查看>>
(C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误
查看>>
flex布局
查看>>
python-----python的文件操作
查看>>
java Graphics2d消除锯齿,使字体平滑显示
查看>>