zoukankan      html  css  js  c++  java
  • django-关系映射 一对一 一对多 多对多

    关系映射

    什么是关系映射

    • 怎么建表
    • 怎么传入数据
    • 怎么查询数据

    一对一的映射 on_delete



    官网链接
    cascade 是在orm层面进行设置
    models.protect mysql默认

    实例说明

    models生成

    oto.models.py

    from django.db import models
    
    # Create your models here.
    class Author(models.Model):
        name=models.CharField('姓名',max_length=11)
    
    class Wife(models.Model):
        name=models.CharField('姓名',max_length=11)
        author=models.OneToOneField(Author,on_delete=models.CASCADE)
        
    

    迁移

    E:django_video_studymysite2>python manage.py startapp oto
    <class 'list'>
    
    E:django_video_studymysite2>python manage.py makemigrations
    <class 'list'>
    Migrations for 'bookstore':
      bookstoremigrations006_auto_20210813_1917.py
        - Change Meta options on book
        - Alter field pub on book
    Migrations for 'oto':
      otomigrations001_initial.py
        - Create model Author
        - Create model Wife
    
    E:django_video_studymysite2>python manage.py migrate
    <class 'list'>
    Operations to perform:
      Apply all migrations: admin, auth, bookstore, contenttypes, oto, sessions
    Running migrations:
      Applying bookstore.0006_auto_20210813_1917... OK
      Applying oto.0001_initial... OK
    
    E:django_video_studymysite2>
    
    

    数据库内样式

    mysql> desc oto_wife;
    +-----------+-------------+------+-----+---------+----------------+
    | Field     | Type        | Null | Key | Default | Extra          |
    +-----------+-------------+------+-----+---------+----------------+
    | id        | int         | NO   | PRI | NULL    | auto_increment |
    | name      | varchar(11) | NO   |     | NULL    |                |
    | author_id | int         | NO   | UNI | NULL    |                |
    +-----------+-------------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)
    
    mysql> show create table oto_wife;
    +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table    | Create Table                                                                                                                                                                                                                                                                                                                                     |
    +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | oto_wife | CREATE TABLE `oto_wife` (
      `id` int NOT NULL AUTO_INCREMENT,
      `name` varchar(11) NOT NULL,
      `author_id` int NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `author_id` (`author_id`),
      CONSTRAINT `oto_wife_author_id_3d57c7a2_fk_oto_author_id` FOREIGN KEY (`author_id`) REFERENCES `oto_author` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
    +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    

    创建数据

    In [1]: from oto.models import Author,Wife
    
    In [2]: author1=Author.objects.create(name='王老师')
    
    In [3]: #通过类的属性名创建数据
    
    In [4]: wife1=Wife.objects.create(name='王夫人',author=author1)
    mysql> select  *  from oto_wife;
    +----+-----------+-----------+
    | id | name      | author_id |
    +----+-----------+-----------+
    |  1 | 王夫人    |         1 |
    +----+-----------+-----------+
    
    
    In [5]: #通过外键字段名创建数据
    
    In [6]: author2=Author.objects.create(name='果子哥')
    
    In [7]: wife2=Wife.objects.create(name='郭老师',author_id=2)
    mysql> select  *  from oto_wife;
    +----+-----------+-----------+
    | id | name      | author_id |
    +----+-----------+-----------+
    |  1 | 王夫人    |         1 |
    |  2 | 郭老师    |         2 |
    +----+-----------+-----------+
    2 rows in set (0.00 sec)
    
    

    数据查询

    正向查询

    In [7]: wife2=Wife.objects.create(name='郭老师',author_id=2)
    
    In [8]: # 正向查询
    
    In [9]: wife2
    Out[9]: <Wife: Wife object (2)>
    
    In [10]: wife2.author
    Out[10]: <Author: Author object (2)>
    
    In [11]: wife2.author.name
    Out[11]: '果子哥'
    

    反向查询

    In [12]: # 反向查询
    
    In [13]: # Author在onetoone中会有一个反向属性
    
    In [14]: author1
    Out[14]: <Author: Author object (1)>
    
    In [15]: author1.wife.name
    Out[15]: '王夫人'
    
    
  • 相关阅读:
    Linq 中 表连接查询
    Html Div 拖拽
    持续集成:TestNG中case之间的关系
    测试技术培训:如何测试磁盘写的速度
    POPTEST 测试开发 免费培训课程报名
    接上文 下面是一段示例代码
    老李分享:android手机测试之适配(1)
    (转)POPTEST创始人李爱然:谢谢,帮助我的朋友!!!!
    性能调优之SQL优化
    大数据测试之Hadoop的基本概念
  • 原文地址:https://www.cnblogs.com/yescarf/p/15138916.html
Copyright © 2011-2022 走看看