【问题标题】:'dict' object has no attribute friend'dict' 对象没有属性friend
【发布时间】:2012-01-12 13:34:05
【问题描述】:

我收到此错误:渲染时捕获 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 ''

【问题讨论】:

    标签: django django-models django-templates


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 2021-03-08
      • 2017-05-12
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 2021-01-30
      相关资源
      最近更新 更多