'dict'对象没有属性朋友

ben*_*eni 3 django django-templates django-models

我收到此错误:渲染时捕获AttributeError:'dict'对象没有属性'友谊'.问题是当我尝试在自定义模板标记中获取friend.friend的值时.模块'friends_for_user'是对的.我有:

楷模

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
Run Code Online (Sandbox Code Playgroud)

模板标签

 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 ''
Run Code Online (Sandbox Code Playgroud)

squ*_*den 7

看起来你正在使用字典而不是对象.尝试potential_friends.append(Friendship.objects.friends_for_user(朋友['朋友']))