本文介绍了Django 1.8 KeyError:关系中的“经理”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设置了这样的模型:
class Post(models.Model):
name = models.CharField(max_length=10)
class Comment(models.Model):
post = models.ForeignKey(Post,related_name='comments')
name = models.CharField(max_length=10)
当我想从帖子中获取所有评论 somepost.comments()
我收到以下错误:
And when I want to get all the comments from a post somepost.comments()
I get the following error:
>> somepost.comments()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "a_path/django/db/models/fields/related.py", line 693, in __call__
manager = getattr(self.model, kwargs.pop('manager'))
KeyError: 'manager'
推荐答案
应该是code> somepost.comments.all()。
It should be somepost.comments.all()
.
somepost.comments
返回一个查询。 全部
访问其中的对象。
somepost.comments
returns a queryset. all
accesses the objects in it.
这篇关于Django 1.8 KeyError:关系中的“经理”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!