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

    一对多

    定义

    语法

    示例

    模型类的创建

    otm/models.py

    from django.db import models
    
    # Create your models here.
    class   Publisher(models.Model):
        name=models.CharField('出版社',max_length=20)
    
    class Book(models.Model):
        title=models.CharField('书名',max_length=11)
        publisher=models.ForeignKey(Publisher, on_delete=models.CASCADE)
    

    数据库显示内容

    mysql> desc otm_book;
    +--------------+-------------+------+-----+---------+----------------+
    | Field        | Type        | Null | Key | Default | Extra          |
    +--------------+-------------+------+-----+---------+----------------+
    | id           | int         | NO   | PRI | NULL    | auto_increment |
    | title        | varchar(11) | NO   |     | NULL    |                |
    | Publisher_id | int         | NO   | MUL | NULL    |                |
    +--------------+-------------+------+-----+---------+----------------+
    3 rows in set (0.40 sec)
    
    mysql> desc otm_publisher;
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int         | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(20) | NO   |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    2 rows in set (0.01 sec)
    
    mysql> show create table otm_book;
    +----------+--------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    ---+
    | Table    | Create Table
    
    
    
    
       |
    +----------+--------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    ---+
    | otm_book | CREATE TABLE `otm_book` (
      `id` int NOT NULL AUTO_INCREMENT,
      `title` varchar(11) NOT NULL,
      `Publisher_id` int NOT NULL,
      PRIMARY KEY (`id`),
      KEY `otm_book_Publisher_id_4ca7afa5_fk_otm_publisher_id` (`Publisher_id`),
      CONSTRAINT `otm_book_Publisher_id_4ca7afa5_fk_otm_publisher_id` FOREIGN KEY (`
    Publisher_id`) REFERENCES `otm_publisher` (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
    +----------+--------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    ---+
    1 row in set (0.05 sec)
    
    mysql>
    

    数据创建

    shell

    In [5]: p1=Publisher(name='南京大学出版社')
    
    In [6]: p1.save()
    
    In [7]: Book.objects.create(title='java',publisher=p1)
    Out[7]: <Book: Book object (1)>
    
    In [8]: p2=Pubilisher.objects.create(name='杭州大学出版社')
    
    In [10]: Book.objects.create(title='java',publisher_id=2)
    Out[10]: <Book: Book object (2)>
    
    In [11]: Book.objects.create(title='django',publisher=p2)
    Out[11]: <Book: Book object (3)>
    
    

    数据查询


    正向查询

    In [19]: book1=Book.objects.get(id='1')
    
    In [20]: book1.publisher.name
    Out[20]: '南京大学出版社'
    
    

    反向查询 注意是类名小写加一个下划线set(如 book_set)

    In [38]: pub2=Publisher.objects.get(id=4)
    
    In [39]: pub2.name
    Out[39]: '杭州大学出版社'
    
    In [40]: pub2.book_set.all()
    Out[40]: <QuerySet [<Book: Book object (3)>]>
    
    
    In [42]: pub2.book_set.all().title
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-42-be1e4f3f46eb> in <module>
    ----> 1 pub2.book_set.all().title
    
    AttributeError: 'QuerySet' object has no attribute 'title'
    
    In [43]: for i in pub2.book_set.all():
        ...:     print(i.title)
        ...:
    django
    

    多对多

    image

    表的创建

    image

    mtm/models.py

    from django.db import models
    
    # Create your models here.
    class Author(models.Model):
        name=models.CharField('姓名',max_length=11)
    
    class Book(models.Model):
        title=models.CharField('书名',max_length=11)
        author=models.ManyToManyField(Author)
    
    
    mysql> desc mtm_author;
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int         | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(11) | NO   |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)
    
    mysql> desc mtm_book;
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int         | NO   | PRI | NULL    | auto_increment |
    | title | varchar(11) | NO   |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)
    
    mysql> desc mtm_book_author;
    +-----------+------+------+-----+---------+----------------+
    | Field     | Type | Null | Key | Default | Extra          |
    +-----------+------+------+-----+---------+----------------+
    | id        | int  | NO   | PRI | NULL    | auto_increment |
    | book_id   | int  | NO   | MUL | NULL    |                |
    | author_id | int  | NO   | MUL | NULL    |                |
    +-----------+------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)
    
    

    数据创建

    image

    In [1]: from mtm.models import Book,Author
    
    In [2]: author1=Author.objects.create(name='吕老师')
    
    In [3]: author2=Author.objects.create(name='王老师')
    
    In [4]: book1=author1.book_set.create(title='python')
    
    In [5]: author2.book_set.add(book1)
    
    In [7]: #先创建book,再关联author
    
    In [8]: book2=Book.objects.create(title='django')
    
    In [9]: #关联两个老师
    
    In [10]: author3=book2.author.create(name='李老师')
    
    In [11]: #再关联一位老师
    
    In [12]: author3
    Out[12]: <Author: Author object (3)>
    
    In [13]: book2.author.add(author1)
    
    In [14]: book2
    Out[14]: <Book: Book object (2)>
    
    

    数据查询

    image
    image

  • 相关阅读:
    ZeptoLab Code Rush 2015
    UVa 10048 Audiophobia【Floyd】
    POJ 1847 Tram【Floyd】
    UVa 247 Calling Circles【传递闭包】
    UVa 1395 Slim Span【最小生成树】
    HDU 4006 The kth great number【优先队列】
    UVa 674 Coin Change【记忆化搜索】
    UVa 10285 Longest Run on a Snowboard【记忆化搜索】
    【NOIP2016提高A组模拟9.28】求导
    【NOIP2012模拟10.9】电费结算
  • 原文地址:https://www.cnblogs.com/yescarf/p/15142165.html
Copyright © 2011-2022 走看看