zoukankan      html  css  js  c++  java
  • Django Models 查询操作

    1.准备数据表:

    from django.db import models
    
    class City(models.Model):
    name=models.CharField(max_length=32)
    nid=models.AutoField(primary_key=True)
    
    class Author(models.Model):
    name=models.CharField(max_length=32)
    nid=models.AutoField(primary_key=True)
    city=models.ForeignKey(to=City,on_delete=models.CASCADE)
    
    class Book(models.Model):
    nid=models.AutoField(primary_key=True)
    title=models.CharField(max_length=32)
    author=models.ManyToManyField(to=Author) #中间表,不用写on_delete
    
    class info(models.Model):
    telephone=models.IntegerField()
    nid=models.AutoField(primary_key=True)
    author=models.OneToOneField(to=Author,on_delete=models.CASCADE) #数据库中增加了字段author_id

    2.插入数据:

    models.City(name='losange').save()
    models.City(name='shanghai').save()
    
    models.Book.objects.create(title='liao story')
    models.Book.objects.create(title='spring')
    models.Book.objects.create(title='old man and sea') 
    models.Book.objects.create(title='the sun rises')
    models.Book.objects.create(title='cats')
    models.Book.objects.create(title='lions')
    
    city=models.City.objects.first()
    models.Author.objects.create(name='haimingwei',city=city)
    city=models.City.objects.all()[1]
    models.Author.objects.create(name='xuewen',city=city)

    3.查询操作


    #1.跨表多对多查询
    #模型关系 城市 <-- 作者 <-- 书

    #查询haimingwei写的所有书籍
    #基于对象-->反向查询(按表名_set) (返回queryset)
    models.Author.objects.get(name='haimingwei').book_set.values('title')
    #基于queryset-->反向查询(按表名) (返回queryset)
    models.Author.objects.filter(name='haimingwei').values('book__title')
    #基于queryset-->正向查询(按字段) (返回queryset)
    models.Book.objects.filter(author__name='haimingwei').values('title')

    #查询spring这本书的作者
    #基于对象-->正向查询(按字段) (返回queryset)
    models.Book.objects.get(title='spring').author.values('name')
    #基于queryset-->反向查询(按表名) (返回queryset)
    models.Author.objects.filter(book__title='spring').values('name')
    #基于queryset-->正向查询(按字段) (返回queryset)
    models.Book.objects.filter(title='spring').values('author__name')

    #2.跨表一对多查询 #模型关系 城市 <-- 作者 <-- 书
    #查询shanghai所有的作者
    #基于对象 --> 反向查询(按表名_set) (返回queryset)
    models.City.objects.get(name='shanghai').author_set.values('name')
    #基于queryset --> 反向查询(按表名)
    models.City.objects.filter(name='shanghai').values('author__name')
    #基于queryset --> 正向查询(按字段)(返回queryset)
    models.Author.objects.filter(city__name='shanghai').values('name')

    #查询海明威所在的城市
    #基于对象 --> 正向查询 (按字段) (返回object)
    models.Author.objects.get(name='haimingwei').city.name
    #基于queryset --> 反向查询(按表名)
    models.City.objects.filter(author__name='haimingwei').values('name')
    ##基于queryset --> 正向查询(按字段)
    models.Author.objects.filter(name='haimingwei').values('city__name')

    #3.跨表一对一查询
    #模型关系 作者 <-- 作者信息

    #查询手机号为123的作者
    #基于对象 --> 正向查询 (按字段) (返回object)
    models.info.objects.get(telephone=123).author.name
    #基于queryset --> 正向查询(按字段)
    models.info.objects.filter(telephone=123).values('author__name')
    #基于queryset --> 反向查询(按表名)
    models.Author.objects.filter(info__telephone=123).values('name')

    #查询haimingwei的手机号
    #基于对象 --> 反向查询 (按表名) (返回object!!!)
    models.Author.objects.get(name='haimingwei').info.telephone
    #基于queryset --> 正向查询(按字段)
    models.info.objects.filter(author__name='haimingwei').values('telephone')
    #基于queryset --> 反向查询(按表名)
    models.Author.objects.filter(name='haimingwei').values('info__telephone')


    #查询写old man and sea这本书作者所在的城市 -->跨三张表查询!
    #models.City.objects.filter(author__book__title='old man and sea').values('name')
    #models.Book.objects.filter(title='old man and sea').values('author__city__name')
    #models.Author.objects.filter(book__title='old man and sea').values('city__name')


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

    #聚合查询 --> 返回一个字典
    models.info.objects.aggregate(total=Sum('telephone'))


    #分组查询 -->不管是单表还是跨表一律都是基于queryset的查询
    #模型关系 城市 <-- 作者 <-- 书

    #查询每个作者写的书籍总数 -->作者作为基表
    models.Author.objects.values('name').annotate(each_total=Count('book__title')).values('name','each_total')
    #查询每个作者写的书籍总数 -->书作为基表
    models.Book.objects.values('author__name').annotate(each_total=Count('title')).values('each_total')

    #查询每本书的作者个数
    models.Book.objects.values('title').annotate(total=Count('author__nid'))
    models.Author.objects.values('book__title').annotate(total=Count('nid'))

    #查询不止一个作者的书籍
    models.Book.objects.values('nid').annotate(c=Count('author__nid')).filter(c__gt=1).values('title','c')


    #F查询
    #比较不同字段值
    models.info.objects.filter(telephone__gt=F('nid'))
    models.info.objects.filter(telephone__gt=F('nid')*2)
    #修改字段值
    models.info.objects.update(telephone=F('telephone')*2).values('telephone')


    #Q查询
    #查询xuewen或海明威所写的书
    models.Book.objects.filter(Q(author__name='haimingwei')|Q(author__name='xuewen')).values('title')

    总结:

    • 查询返回多个对象时,返回值为queryset;
    • 基于对象反向查询时,表名_set!
    • 确定返回一个对象时,返回值为object!
  • 相关阅读:
    2.5 进程控制之wait函数
    2.4 进程控制之僵尸进程和孤儿进程
    九、IIC驱动原理分析
    2.3 进程控制之exec函数族
    8.2 USB键盘驱动编写和测试
    1. tty终端接收数据原理
    第3章 MySQL常用增,删,改,查,用户授权,备份,等操作
    Linux命令总结--top命令
    Linux--LNMP
    Linux命令总结-ps pstree pgrep命令
  • 原文地址:https://www.cnblogs.com/liaoxuewen/p/10308984.html
Copyright © 2011-2022 走看看