Home > Enterprise >  'method' object does not support item assignment
'method' object does not support item assignment

Time:09-26

I am trying to count likes on each post and this is my model. But I am getting error on the item detail page

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


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

VIEW:

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

CodePudding user response:

You forgot to call get_context_data. Change it to:

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

  • Related