django-关系映射

Posted yescarf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于django-关系映射的主要内容,如果未能解决你的问题,请参考以下文章

Django的URLconf

20.Django模型映射关系

20.Django模型映射关系

python-- Django路由系统(网址关系映射)视图模板

django-关系映射

Django的Models映射关系