zoukankan      html  css  js  c++  java
  • day72

    今日内容:
     1 创建多表模型(详情见代码)

    from django.db import models
    
    
    # Create your models here.
    
    class Publish(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=32)
        addr = models.CharField(max_length=64)
        email = models.EmailField(null=True)
    
    
    class AuthorDetail(models.Model):
        id = models.AutoField(primary_key=True)
        sex = models.IntegerField()
        phone = models.CharField(max_length=11)
    
    
    class Author(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=32)
        addr = models.CharField(max_length=64)
        author_details = models.OneToOneField(to='AuthorDetail', to_field='id')
    
    
    class Book(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=32)
        price = models.DecimalField(max_digits=5, decimal_places=2)
        publish = models.ForeignKey(to='Publish', to_field='id')
        authors = models.ManyToManyField(to=Author)

          #用了OneToOneField和ForeignKey,模型表的字段,后面会自定加_id
          # ManyToManyField会自动创建第三张表
          # *************重点
          # 一对一的关系:OneToOneField
          # 一对多的关系:ForeignKey
          # 多对多的关系:ManyToManyField
      
     2 添加表记录
        1 一对多新增
           -两种方式:
              -publish=对象
              -publish_id=id

        # 一对多增
        # 方式一
        publish = models.Publish.objects.filter(name='北京出版社').first()
        res= models.Book.objects.create(name='红楼梦',price=55.67,publish=publish)
        # 方式二
        res = models.Book(name='三国演义',price=73.89)
        res.publish_id=2
        res.save()
        # 方式三
        res = models.Book.objects.create(name='西游记',price=65,publish_id=4)
    

      

        2 一对多删除:同单表删除

    res = models.Publish.objects.filter(id=4).delete()
    #由于Publish表与Book表的关系是一对多,所以一旦删除Publish表中的记录,会级联删除Book表中的记录
    res = models.Book.objects.filter(name='西游记',price=65).delete()


        3 一对多修改:两种方式,可以传对象,可以传id

        4 一对一跟一对多一样

        # 一对多修改
        # 方式一
        publish = models.Publish.objects.filter(name='东京出版社').first()
        res = models.Book.objects.filter(publish=publish).update(price=99)
        # 方式二
        res = models.Book.objects.filter(publish_id=3).update(price=58)
        # 方式三
        res = models.Book.objects.filter(publish_id=3).first()
        res.price = 73.59
        res.save()

        

      5 多对多:
           -add  ----->可以传对象,可以传id,可以传多个
           -remove  ----->可以传对象,可以传id,可以传多个
           -clear  ---->没有参数
           -set   ----->跟上面不一样,必须传列表,列表里面可以是对象,可以是id

        # 多对多增
        # 方式一
        book = models.Book.objects.filter(name='红楼梦').first()
        user1 = models.Author.objects.filter(id=1).first()
        user2 = models.Author.objects.filter(id=2).first()
        book.authors.add(user1, user2)
        # 方式二
        book = models.Book.objects.filter(name='三国演义').first()
        book.authors.add(2,3)
    
    
        # 多对多删除
        # 方式一
        book = models.Book.objects.filter(name='三国演义').first()
        user = models.Author.objects.filter(id=3).first()
        book.authors.remove(user)
        # 方式二
        book = models.Book.objects.filter(name='红楼梦').first()
        book.authors.remove(3)
    
        # clear
        book = models.Book.objects.filter(name='红楼梦').first()
        book.authors.clear()
    
        # set
        book = models.Book.objects.filter(name='红楼梦').first()
        book.authors.set([1,2,3])


     3 基于对象的跨表查询
        1 一对一
           正向:正向查询按字段
           反向:反向查询按表名小写

        # 一对一
        # 正向
        author = models.Author.objects.filter(id=1).first()
        res = author.author_details
        print(res.phone)
        # 反向
        authordetail = models.AuthorDetail.objects.filter(id=3).first()
        res = authordetail.author
        print(res.addr)


        2 一对多
           正向:正向查询按字段
           反向:反向按表名小写_set.all()

        # 一对多
        # 正向
        book = models.Book.objects.filter(id=2).first()
        res = book.publish
        print(res)
        # 反向
        publish = models.Publish.objects.filter(name='北京出版社').first()
        res = publish.book_set.all()
        print(res)


        3 多对多
           正向:正向查询按字段
           反向查询:反向按表名小写_set.all()

        # 多对多
        # 正向
        book = models.Book.objects.filter(id=2).first()
        res = book.authors.all()
        print(res)
        # 反向
        author = models.Author.objects.filter(id=1).first()
        res = author.book_set.all()
        print(res)


        4******基于对象的查询,多次查询(子查询)
     

        publish = models.Publish.objects.filter(id=1).first()
        book = publish.book_set.filter(name='红楼梦').first()
        author = book.authors.filter(name='郑棒').first()
        authordetail = author.author_details
        print(authordetail.phone)


     
        4 基于双下划线的跨表查询 
      -连表查询
      -一对一双下划线查询
       -正向:按字段,跨表可以在filter,也可以在values中

        res = models.Author.objects.filter(name='郑棒').values('author_details__phone', 'author_details__sex')
        print(res)


       -反向:按表名小写,跨表可以在filter,也可以在values中

        res = models.AuthorDetail.objects.filter(author__name='郑棒').values('phone','author__addr')
        print(res)
    

      

  • 相关阅读:
    SpringCloud 学习之概述
    定位慢查询
    中止线程
    笨办法41学会说面向对象【pyinstaller安装使用
    pip安装
    笨办法40模块, 类和对象class
    笨办法39字典dict
    笨办法38列表操作
    笨办法35分支和函数
    笨办法34访问列表元素(列表方法)
  • 原文地址:https://www.cnblogs.com/yaoxiaofeng/p/9953618.html
Copyright © 2011-2022 走看看