【问题标题】:'method' object does not support item assignment“方法”对象不支持项目分配
【发布时间】:2021-11-18 08:56:09
【问题描述】:

我正在尝试计算每个帖子的点赞数,这是我的模型。但是我在项目详细信息页面上遇到错误

likes = models.ManyToManyField(User, related_name='blog_posts')


    def total_likes(self):
        return self.likes.count()

查看:

class ItemDetailView(DetailView):
    model = Item
    template_name ='waqart/item_detail.html'

    
    def get_context_data(self, *args, **kwargs):
        context = super(ItemDetailView, self).get_context_data

        stuff = get_object_or_404(Item, id=self.kwargs['pk'])
        total_likes = stuff.total_likes()
        context['total_likes']= total_likes
        return context

【问题讨论】:

    标签: python django detailview


    【解决方案1】:

    你忘了打电话给get_context_data。将其更改为:

    context = super(ItemDetailView, self).get_context_data(*args, **kwargs)

    【讨论】:

    • 有没有办法可以在管理面板中显示每个项目的总点赞数?因为我喜欢的字段是 ManyToManyField。提前欣赏
    猜你喜欢
    • 1970-01-01
    • 2015-03-09
    • 2019-01-03
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    相关资源
    最近更新 更多