related_name
django还有一种通过对象访问相关联表数据的方法,即用_set。但是这种方法只能是相关类访问定义了关系的类(主键类访问外键类)。如:b.entry_set.all() #b是一个blog对象
另外还有就是如果Entry中blog属性的定义如果改成这样:blog=ForeignKey(Blog,related_name='entries')这样的话就可以像下面这样通过blog对象得到entry对象的一个集合:
>>> b=Blog.objects.get(id=1)
>>> b.entries.all() #==b.entry_set.all()# b.entries is a Manager that returns QuerySets.
>>> b.entries.filter(headline__contains='Lennon')
>>> b.entries.count()