Django 'int' object is not iterable when trying to update objects in views

I am getting error when trying to update objects from False to True. here is my code:

class ListNoti(LoginRequiredMixin,ListView):
      raise_exception = True
      model = Notifications
      template_name = 'notifications/notifications.html'
      
      def get_context_data(self, **kwargs):
          data = super(ListNoti,self).get_context_data(**kwargs)
          data['noti'] = Notifications.objects.filter(receiver=self.request.user,is_seen=False).order_by('-date').update(is_seen=True)
          
          return data

.update(is_seen=True) raising this error TypeError at /notification/ 'int' object is not iterable

Please post the complete error traceback from the console, along with your Notifications model and the notifications.html template.

2 Likes

KenWhitesell I solved this issue after using queryset in my views. here is my full code:

class ListNoti(LoginRequiredMixin,ListView):

      raise_exception = True

      model = Notifications

      template_name = 'notifications/notifications.html'

      def get_queryset(self):

        data =   Notifications.objects.filter(receiver=self.request.user).update(is_seen=True)

        return data

     

      def get_context_data(self, **kwargs):

          data = super(ListNoti,self).get_context_data(**kwargs)

           

          data['noti'] = Notifications.objects.filter(receiver=self.request.user)

          return data

I am not understanding why filter was not working in get_context_data?

The filter was working fine, the problem was somewhere else.

You need to be aware of the data type being returned by functions being called, and pay attention to what data type you need to return from a function.

1 Like