gpt4 book ai didi

django - “dict”对象没有 friend 属性

转载 作者:行者123 更新时间:2023-12-03 03:38:16 26 4
gpt4 key购买 nike

我收到此错误:渲染时捕获 AttributeError:“dict”对象没有属性“friendship”。问题是当我尝试在自定义模板标记中获取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 ''

最佳答案

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

关于django - “dict”对象没有 friend 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com