保持前面的不变只是增加了一些
1、vim sdj/models.py
class Blog(models.Model):
name = models.CharField(max_length=30)
entry = models.ForeignKey(Employee)
def __unicode__(self):
return self.name
所谓的多对一:多指的是新创建的Blog,一指之前的Employee
2、记得再做一次数据库的同步
python manage.py syncdb
3、python manage.py shell
from sdj.models import Employee, Blog
e1=Employee.objects.create(name='bob1')
e2=Employee.objects.create(name='bob2')
blog = Blog.objects.create(name='bob1_blog',e1='entry')
>>> blog
<Blog: bob_blog>
>>> blog.entry
<Employee: bob>
>>> blog.entry.id
4L
>>> entry1.blog_set
<django.db.models.fields.related.RelatedManager object at 0x1ceef10>
>>> entry1.blog_set.all() 就像一个结果集合
[<Blog: bob_blog>]