首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“‘dict”对象没有属性friend

“‘dict”对象没有属性friend
EN

Stack Overflow用户
提问于 2011-12-04 18:28:28
回答 1查看 3.5K关注 0票数 3

我收到这个错误:渲染时捕获了友情:‘AttributeError’对象没有‘友情’属性。问题是当我试图在自定义模板标记中获取friend.friend的值时。模块'friends_for_user‘是正确的。我有:

模型

代码语言:javascript
复制
class FriendshipManager(models.Manager):

  def friends_for_user(self, user):
     friends = []
     for friendship in self.filter(from_user=user).select_related(depth=1):
        friends.append({"friend": friendship.to_user, "friendship": friendship})
     for friendship in self.filter(to_user=user).select_related(depth=1):
        friends.append({"friend": friendship.from_user, "friendship": friendship})
     return friends

模板标签

代码语言:javascript
复制
 def render(self, context):
    user = self.user.resolve(context)
    num = self.num.resolve(context)

    my_friends = Friendship.objects.friends_for_user(user)

    potential_friends = []
    for friend in my_friends:
        potential_friends.append(Friendship.objects.friends_for_user(friend.friend))  //This line is the error.

    context[self.context_name] = potential_friends
    return ''
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-04 18:33:37

看起来您使用的是字典,而不是对象。试试potential_friends.append(Friendship.objects.friends_for_user(friend'friend'))

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8374674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档