zoukankan      html  css  js  c++  java
  • 【django之orm小练习】

    作业1

    创建单表Book表,要求字段:

    1 主键 nid
    2 书名 title
    3 价格 price
    4 出版日期 pubDate
    5 出版社 publisher(普通字符串字段)

    class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    publishDate = models.DateTimeField()
    publisher = models.CharField(max_length=64)

    作业2

    添加多条记录后,

    查询:

    (1) 查询所有书籍的名称
    
    Book.objects.all().values("title")
    (2) 查询价格大于100的书籍名称以及价格
    
    Book.objects.filter(price__gt=100).values("title","price")
    (3)    查询2017年出版的所有以py开头的书籍名称
    
    Book.objects.filter(pubDate__year="2017",title__startswith="py").values("title")
    
    
    (4) 查询价格为50,100或者150的所有书籍名称及其出版社名称
    
    Book.objects.filter(price__in=[50,100,150]).values("title","pubDate")
    (5)    查询所有主键大于2小于5书籍的价格(去重)
    
    Book.objects.filter(nid__gt=2,nid__lt=5).values("price").distinct()
    (6) 查询所有人民出版社出版的书籍的价格(从高到低排序,去重)
    
    Book.objects.filter(publisher="人民出版社").values("price").order_by("-price").distinct()

    作业3

    models.py

    class Book(models.Model):
        nid = models.AutoField(primary_key=True)
        title = models.CharField(max_length=32)
        publishDate = models.DateField()
        price = models.DecimalField(max_digits=5, decimal_places=2)
    
    
        read_num=models.IntegerField(default=0)
        commnet_num=models.IntegerField(default=0)
        poll_num=models.IntegerField(default=0)
    
        # publish:与当前书籍对象关联的的出版社对象,与Publish建立一对多的关系,外键字段建立在多的一方
        publish=models.ForeignKey(to="Publish",to_field="id")
    
        # authors: 与当前书籍关联的所有作者的集合
        #  与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表
    
        authors=models.ManyToManyField(to="Author")
        def __str__(self):
            return self.title
    class Author(models.Model):
        name=models.CharField(max_length=32)
        age=models.IntegerField()
    
    class AuthorDetail(models.Model):
        tel=models.CharField(max_length=32)
        email=models.CharField(max_length=32)
    
        #与Author建立一对一的关系
        author=models.OneToOneField("Author")
    
    
    
    class Publish(models.Model):
        name=models.CharField(max_length=32)
        email=models.CharField(max_length=32)
    
        def __str__(self):
            return self.name
    models.py
    基于图书管理系统的表关系与数据,进行如下查询
      要求:1-5查询分别基于对象查询,双下划线查询,以及找到对应翻译的sql语句
       
    
      1、查询人民出版社出版过的价格大于100的书籍的作者的email
         Book.objects.filter(publish__name="人民出版社",price__gt=100).values("authors__authordetail__email")
      2、查询alex出版过的所有书籍的名称以及书籍的出版社的名称
         Book.objects.filter(authors__name="alex").values("title","publish__name")
      3、查询2011年出版社过的所有书籍的作者名字以及出版社名称
         Book.objects.filter(publishDate__year=2011).values("publish__name","authors__name")
      4、查询住在沙河并且email以123开头的作者写过的所有书籍名称以及书籍的出版社名称
         Author.objects.filter(authordetail__addr="沙河",authordetail__email__startswith="123").values("book__title","book__publish__name")
      5、查询年龄大于20岁的作者在哪些出版社出版过书籍
         Publish.objects.filter(book__authors__age__gt=20).values("name)
      6、查询每一个出版社的名称以及出版过的书籍个数
         Publish.objects.all().annotate(c=Count("book__id")).values("name","c")
      7、查询每一个作者的名字以及出版过的所有书籍的最高价格
         Author.objects.all().annotate(m=Max("book__price")).values("name","m")
      8、查询每一本书的名字,对应出版社名称以及作者的个数
         Book.objects.all().annotate(c=Count("authors")).values("c","title","publish__name")
  • 相关阅读:
    利用事件委托实现用户控件中的行为触发所在页面的处理函数
    mootools系列:打造属于你自己的Popup(弹出框)——基本结构篇
    mootools系列:打造属于你自己的Popup(弹出框)——外观及应用篇
    所见即所得的Excel报表生成(二)——从Html table到Excel Cell
    Excel操作服务器端配置
    顺丰单号生成规则
    代理模式Proxy
    解释器模式Interpreter
    线性表
    总有一款合适的框架
  • 原文地址:https://www.cnblogs.com/smallmars/p/8422928.html
Copyright © 2011-2022 走看看