您的位置:首页 > 编程语言 > Python开发

Python系列视频教程: Django【13讲】第11讲 数据库多对多映射

2014-02-12 22:06 1191 查看
Python系列视频教程: Django【13讲】第11讲 数据库多对多映射

前面我们看了通过admin的界面,对数据库进行CRUD操作。

今天我们再来看看,如何在数据库里面进行多对多映射



这次我们创建csvt06和应用blog

修改settings.py



修改blog\models.py



from django.db import models

# Create your models here.

class Author(models.Model):

name=models.CharField(max_length=30)



def __unicode__(self):

return self.name

class Book(models.Model):

name=models.CharField(max_length=30)

authors=models.ManyToManyField(Author)



def __unicode__(self):

return self.name





python manage.py syncdb

jian.zhang/~cajan



进入到解释器环境

sqlite3 csvt06.sqlite3

可以看到三张blog的表已经有了

python manage.py shell

D:\test\mysite\csvt06>python manage.py shell

Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win

32

Type "help", "copyright", "credits" or "license" for more information.

(InteractiveConsole)

>>> from blog.models import Author,Book

>>> Author.objects.create(name='Alen')

<Author: Alen>

>>> Author.objects.create(name='Ben')

<Author: Ben>

>>> Author.objects.create(name='Cool')

<Author: Cool>

>>> Author.objects.create(name='Dart')

<Author: Dart>

>>> authors=Author.objects.all()

>>> authors

[<Author: Alen>, <Author: Ben>, <Author: Cool>, <Author: Dart>]

>>> b1=Book()

>>> b1.name='python book1'

>>> b1

<Book: python book1>

>>> b1.save()

>>> author1=Author.objects.get(name='Alen')

>>> author1

<Author: Alen>

>>> b1.authors.add(author1)

>>> b1

<Book: python book1>

>>> b1.authors

<django.db.models.fields.related.ManyRelatedManager object at 0x019AE630>

>>> b1.authors(1)

Traceback (most recent call last):

File "<console>", line 1, in <module>

TypeError: 'ManyRelatedManager' object is not callable

>>> b1.authors.all()

[<Author: Alen>]

>>> author2=Author.objects.get(name='Ben')

>>> b1.authors.add(author2)

>>> b1.authors.all()

[<Author: Alen>, <Author: Ben>]

>>> author3=Author.objects.get(name='Cool')

>>> b1.authors.add(author3)

>>> b1.authors.all()

[<Author: Alen>, <Author: Ben>, <Author: Cool>]

>>> b1.authors.remove(author1)

>>> b1.authors.all()

[<Author: Ben>, <Author: Cool>]

>>>

除了all()还可以使用filter(=)过滤

>>> b1.authors.filter(name__exact='Cool')

[<Author: Cool>]

>>>

使用get(=)获取

>>> b1.authors.get(name='Cool')

<Author: Cool>

>>>

我们还可以使用book_set

>>> author2.book_set

<django.db.models.fields.related.ManyRelatedManager object at 0x019B2E10>

>>> author2.book_set.all()

[<Book: python book1>]

>>>

>>> author1.book_set.all()

[]

>>> author1.book_set.add(b1)

>>> author1.book_set.all()

[<Book: python book1>]

>>>

>>> author1.book_set.create(name='python book2')

<Book: python book2>

>>> author1.book_set.all()

[<Book: python book1>, <Book: python book2>]

>>>

>>> books=Book.objects.all()

>>> books

[<Book: python book1>, <Book: python book2>]

>>>

>>> author1.book_set.remove(books[0])

>>> books

[<Book: python book1>, <Book: python book2>]

>>> author1.book_set.all()

[<Book: python book2>]

>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: