2011-12-04 74 views
3

我收到此錯誤:在呈現時捕獲AttributeError:'字典'對象沒有屬性'友誼'。問題是,當我嘗試獲取自定義模板標記中的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 

模板標籤

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 '' 

回答

7

它看起來像您使用的是字典不是一個對象。 嘗試 potential_friends.append(Friendship.objects.friends_for_user(friend ['friend']))

+0

是的。謝謝。 – beni

相關問題