Go教程

django-关系映射

本文主要是介绍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-关系映射的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!