Django的多对多关系
我们首先来设计一个用于示例的表结构
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Book(models.Model):
name = models.CharField(max_length = 32)
def __str__(self):
return self.name.decode("utf-8")
def __unicode(self):
return self.name.decode("utf-8")
class Author(models.Model):
name = models.CharField(max_length = 32)
m = models.ManyToManyField('Book')
def __str__(self):
return self.name.decode("utf-8")
def __unicode(self):
return self.name.decode("utf-8")
In [1]: from app_many_models_01 import models
In [2]: models.Author.objects.create(name = u'张三')
Out[2]: <Author: 张三>
In [3]: models.Author.objects.create(name = u'赵一')
Out[3]: <Author: 赵一>
In [4]: models.Author.objects.create(name = u'张六')
Out[4]: <Author: 张六>
In [5]: models.Author.objects.create(name = u'李七')
Out[5]: <Author: 李七>
In [6]: models.Book.objects.create(name = u'python编程')
Out[6]: <Book: python编程>
In [7]: models.Book.objects.create(name = u'Go编程')
Out[7]: <Book: Go编程>
In [8]: models.Book.objects.create(name = u'Java编程')
Out[8]: <Book: Java编程>
In [9]: models.Book.objects.create(name = u'shell编程')
Out[9]: <Book: shell编程>
In [10]: models.Book.objects.create(name = u'Lua编程')
Out[10]: <Book: Lua编程>
In [11]: models.Book.objects.create(name = u'Ruby编程')
Out[11]: <Book: Ruby编程>
In [12]: models.Book.objects.create(name = u'PHP编程')
Out[12]: <Book: PHP编程>
In [13]: models.Book.objects.create(name = u'Perl编程')
Out[13]: <Book: Perl编程>
In [14]: models.Book.objects.create(name = u'AI编程')
Out[14]: <Book: AI编程>
In [1]: from app_many_models_01 import models
In [2]: p = models.Book.objects.get(name = "Python编程")
In [3]: php = models.Book.objects.get(name = "PHP编程")
In [4]: pe = models.Book.objects.get(name = "Perl编程")
In [5]: zhangliu = models.Author.objects.get(name = u"张六")
In [6]: zhang = models.Author.objects.get(name = u"张三")
In [7]: li = models.Author.objects.get(name = u"李七")
In [8]: zhao = models.Author.objects.get(name = u"赵二")
In [18]: zhao.m.add(p)
In [65]: zhang.m.create(name=u"Basic编程")
Out[65]: <Book: Basic编程>
In [66]: zhang.m.add(php)
In [67]: zhang.m.add(pe)
In [68]: zhang.m.add(p,php,pe)
In [69]: zhang.m.add(p,php,pe,go)
In [73]: zhang.m.create(name=u"vb编程")
Out[73]: <Book: vb编程>
In [74]: b = models.Book.objects.get(name = u"vb编程" )
In [75]: b.author_set.all()
Out[75]: <QuerySet [<Author: 张三>]>
mysql> use test_many_models;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from app_many_models_01_author;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
| 2 | 赵一 |
| 3 | 张六 |
| 4 | 李七 |
+----+--------+
4 rows in set (0.00 sec)
mysql> select * from app_many_models_01_author_m;
+----+-----------+---------+
| id | author_id | book_id |
+----+-----------+---------+
| 11 | 1 | 1 |
| 16 | 1 | 2 |
| 14 | 1 | 7 |
| 15 | 1 | 8 |
| 12 | 1 | 14 |
| 13 | 1 | 15 |
| 17 | 1 | 16 |
| 7 | 3 | 3 |
| 6 | 3 | 9 |
| 18 | 4 | 16 |
+----+-----------+---------+
10 rows in set (0.00 sec)
In [77]: zhang.m.remove(b)
mysql> select id from app_many_models_01_book where name="vb编程";
+----+
| id |
+----+
| 16 |
+----+
1 row in set (0.00 sec)
mysql> select * from app_many_models_01_author_m;
+----+-----------+---------+
| id | author_id | book_id |
+----+-----------+---------+
| 11 | 1 | 1 |
| 16 | 1 | 2 |
| 14 | 1 | 7 |
| 15 | 1 | 8 |
| 12 | 1 | 14 |
| 13 | 1 | 15 |
| 17 | 1 | 16 |
| 7 | 3 | 3 |
| 6 | 3 | 9 |
| 18 | 4 | 16 |
+----+-----------+---------+
10 rows in set (0.00 sec)
mysql> select * from app_many_models_01_author_m;
+----+-----------+---------+
| id | author_id | book_id |
+----+-----------+---------+
| 11 | 1 | 1 |
| 16 | 1 | 2 |
| 14 | 1 | 7 |
| 15 | 1 | 8 |
| 12 | 1 | 14 |
| 13 | 1 | 15 |
| 7 | 3 | 3 |
| 6 | 3 | 9 |
| 18 | 4 | 16 |
+----+-----------+---------+
9 rows in set (0.00 sec)
mysql> select id from app_many_models_01_author where name = "李七";
+----+
| id |
+----+
| 4 |
+----+
1 row in set (0.00 sec)
mysql> select book_id from app_many_models_01_author_m where author_id="4";
+---------+
| book_id |
+---------+
| 16 |
+---------+
1 row in set (0.00 sec)
In [78]: li.m.clear()
我们再来看看现在李七这个作者还有什么书籍?
mysql> select book_id from app_many_models_01_author_m where author_id="4";
Empty set (0.00 sec)