models.py文件内容
class Article(models.Model):
title =models.CharField(max_length=50)
content = models.CharField(max_length=800)
def __unicode__(self):
return self.title
class author(models.Model):
name = models.CharField(max_length=10)
articles = models.ManyToManyField(Article)
def __unicode__(self):
return self.name
使用命令python manager.py shell
导入blog 文件夹
>>>form blog import *
查询该文章的所有作者
>>>models.author.objects.filter(articles__title='title')
<QuerySet [<author: tuweifeng>, <author: none>, <author: b>]>
查询作者所有的文章
>>> models.author.objects.get(name='tuweifeng').articles.all()
<QuerySet [<Article: title>]>
查询该文章名为b的作者
>>> models.author.objects.filter(articles__title='title',name='b')
<QuerySet [<author: b>]>