Django复习之ORM

     
     QuerySet数据类型:
                        1、可切片,可迭代      [obj,....] 
                  2、惰性查询:
               articles_list=models.Article.objects.all()
                              使用articles_list,比如if articles_list,这时转换sql语句    


                        3、缓存机制
               articles_list=models.Article.objects.all()
               for i in articles_list:
                                        print(i.title)  # hit the database
                                
         for i in articles_list:
                                        print(i.title)  # not hit the database 

                                 ==============================================
                                 for i in models.Article.objects.all():
                                        print(i.title)  # hit the database
                                
         for i in models.Article.objects.all():
                                        print(i.title)  #  hit the database 
      4、优化查询
               articles_list=models.Article.objects.all().iterator()
               for i in articles_list:
                                        print(i.title)  # hit the database
          
            for i in articles_list:
                                        print(i.title)  # 无结果   

                                      
        连表操作:
               
                表关系:
           
                       class UserInfo(AbstractUser):  # settings:   AUTH_USER_MODEL = "blog.UserInfo"
       """
       用户信息
       """
       nid = models.BigAutoField(primary_key=True)
       nickname = models.CharField(verbose_name='昵称', max_length=32,null=True)
       telephone = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手机号码')
       avatar = models.FileField(verbose_name='头像', upload_to='avatar', default="avatar/default.png")
       create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

 

      class Blog(models.Model):
       """
       站点信息
       """
       nid = models.BigAutoField(primary_key=True)
       title = models.CharField(verbose_name='个人博客标题', max_length=64)
       site = models.CharField(verbose_name='个人博客后缀', max_length=32, unique=True)
       theme = models.CharField(verbose_name='博客主题', max_length=32)

       user = models.OneToOneField(to='UserInfo', to_field='nid')

       

      class Category(models.Model):
       
       nid = models.AutoField(primary_key=True)
       title = models.CharField(verbose_name='分类标题', max_length=32)
       blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid')

       


      class Article(models.Model):
       nid = models.BigAutoField(primary_key=True)
       title = models.CharField(max_length=50, verbose_name='文章标题')
       desc = models.CharField(max_length=255, verbose_name='文章描述')
       
                            # category字段: 与Article对象关联的category对象
       category = models.ForeignKey(verbose_name='文章类型', to='Category', to_field='nid', null=True)
       
       # user字段:与Article对象关联的user字段
       user = models.ForeignKey(verbose_name='所属用户', to='UserInfo', to_field='nid')
       
       tags = models.ManyToManyField(
        to="Tag",
        through='Article2Tag',
        through_fields=('article', 'tag'),
       )

       
      class ArticleDetail(models.Model):
       
       nid = models.AutoField(primary_key=True)
       content = models.TextField(verbose_name='文章内容', )

       article = models.OneToOneField(verbose_name='所属文章', to='Article', to_field='nid')
                       
      class Article2Tag(models.Model):
       nid = models.AutoField(primary_key=True)
       article = models.ForeignKey(verbose_name='文章', to="Article", to_field='nid')
       tag = models.ForeignKey(verbose_name='标签', to="Tag", to_field='nid')
      
      
      class Tag(models.Model):
       nid = models.AutoField(primary_key=True)
       title = models.CharField(verbose_name='标签名称', max_length=32)
       blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid')

       
      
                关联表的添加记录操作:
            1、创建一篇文章对象:
         user_obj=models.UserInfo.objects.get(nid=1)
         category_obj=models.Category.objects.get(nid=2)
        
        
         #################一对多关系绑定#########################
         # 方式1:
         article_obj=models.Article.objects.create(nid=5,title="招呼亚星"....,user=user_obj,category=category_obj)
        
         # 方式2:
          article_obj=models.Article.objects.create(nid=5,title="招呼亚星"....,user_id=1,category_id=2)
         '''
             Article:
                    nid         title        user_id     category_id
                        5         招呼亚星         1              2
                        
         '''
         #################多对多关系绑定#########################

 if 没有中介模型:
           tags = models.ManyToManyField("Tag")
           '''
              ORM创建的第三张表:
              Article2tags:

                                         nid    article_id    tag_id  
                                          1         5            1
                                          2         5            2           
          
           '''
          
           实例:给article_obj绑定kw1,kw2的标签
            tag1=Tag.objects.filter(title=kw1).first()
            tag2=Tag.objects.filter(title=kw2).first()
           
            article_obj.tags.add(tag1,tag2)   #     
            article_obj.tags.add(*[tag1,tag2])   
            article_obj.tags.add(1,2)   
            article_obj.tags.add(*[1,2]) 
                
            解除关系:
             article_obj.tags.remove(tag1,tag2)
             article_obj.tags.clear()

                  重置关系:
              article_obj.tags.clear()
           article_obj.tags.add(tag1,tag2) 
           =====
           article_obj.tags.set(tag1,tag2)
          
          
          
          
      if 有中介模型:      
                             tags = models.ManyToManyField(
           to="Tag",
           through='Article2Tag',
           through_fields=('article', 'tag'),
          )

        class Article2Tag(models.Model):  #  中介模型
         nid = models.AutoField(primary_key=True)
         article = models.ForeignKey(verbose_name='文章', to="Article", to_field='nid')
         tag = models.ForeignKey(verbose_name='标签', to="Tag", to_field='nid')
        
                             绑定多对多的关系,有中介模型,不能再使用article_obj.tags.add(),remove()等方法;
        只能用Article2Tag表进行实例对象。
       
       
       
        实例:给article_obj绑定kw1,kw2的标签:
       
         models.Article2Tag.objects.create(tag_id=1,article_id=5)
         models.Article2Tag.objects.create(tag_id=2,article_id=5)
        
      
    关联表的查询:
                        
                        基于对象查询(子查询):
         
       一对多的查询:
       
        实例1:查询主键为4的文章的分类名称(正向查询,按字段)
           
            article_obj=models.Article.objects.get(nid=4)
            print(article_obj.category.title)
        
            SELECT * FROM "blog_article" WHERE "blog_article"."nid" = 4;  // category_id=2
            SELECT * FROM "blog_category" WHERE "blog_category"."nid" = 2;

        实例2:查询用户yuan发表过的所有的文章(反向查询,表名_set)
               yuan=models.UserInfo.objects.get(username="yuan")
            book_list=yuan.article_set.all()  # QuerySet
                 
             
       
       多对多的查询:
                               
                                 实例3:查询主键为4的文章的标签名称(正向查询,按字段)  
          article_obj=models.Article.objects.get(nid=4)        
          tag_list=article_obj.tags.all()   #  是通过Article2Tag找到tag表中关联的tag记录
          for i in tag_list:
            print(i.title)
          
         实例4:查询web开发的这个标签对应的所有的文章(反向查询,按表名_set)
                                    tag_obj=models.Tag.objects.get(title="web开发")
         article_list=tag_obj.article_set.all()
         for i in article_list:
          print(i.title)       
            
            实例5:查询web开发的这个标签对应的所有的文章对应的作者名字
                                      tag_obj=models.Tag.objects.get(title="web开发")
           article_list=tag_obj.article_set.all() 
                                      for article in article_list:
             print(article.user.username)                                    
            
            
            
            
            
            
            
      基于QuerySet跨表查询  正向查询,按字段 ;反向查询,按表名(join查询)
      
        一对多的查询:
       
        实例1:查询主键为4的文章的分类名称
           
           models.Article.objects.filter(nid=4).values("category__title")  
                                      models.Category.objects.filter(article__nid=4).values("title")
          
        实例2:查询用户yuan发表过的所有的文章
            models.UserInfo.objects.filter(username="yuan").values(article__title)
            models.Article.objects.filter(user__username="yuan").values("title")
          
       
        多对多的查询:
         
          实例3:查询主键为4的文章的标签名称(正向查询,按字段)  
           models.Article.objects.filter(nid=4).values("tags__title")
           models.Tag.objects.filter(article__nid=4).values("title")         
          
          实例4:查询web开发的这个标签对应的所有的文章(反向查询,按表名_set)
           models.Article.objects.filter(tags__title="web开发").values("title")
           models.Tag.objects.filter(title="web开发").values("article__title")         
            
          实例5:查询web开发的这个标签对应的所有的文章对应的作者名字
            models.Tag.objects.filter(title="web开发").values("article__user__username")
            models.UserInfo.objects.filter(article__tags__title="web开发").values("username")
          
    聚合查询与分组查询:
    
            聚合查询:
          # 查询所有书籍的平均价格
          Book.objects.all().aggregate(Avg('price'))
       
      分组查询:
                            #查询每一个出版社出版过的名字以及出版书籍个数
                           
                            sql: select publish.name,Count(book.name) from Book inner join publish on book.pub_id=publish.id
                    group by publish.name
          
       ORM:   models.Publish.objects.all().annotate(c=Count("book_id")).values("name","c")
                           

转载于:https://www.cnblogs.com/1a2a/p/7975406.html

你可能感兴趣的:(python,数据库)