【问题标题】:Django 'int' object is not iterable when trying to update objects in views尝试更新视图中的对象时,Django 'int' 对象不可迭代
【发布时间】:2022-01-26 18:42:54
【问题描述】:

尝试将对象从 False 更新为 True 时出现此错误。这是我的代码:

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) 引发此错误TypeError at /notification/ 'int' object is not iterable

【问题讨论】:

  • order_by之前移动update(is_seen=True)
  • Ahtisham 试过但没用
  • 检查它是否在没有 order_by 的情况下工作
  • Ahtisham 在发布问题之前我都试过了。没用。

标签: python-3.x django django-views


【解决方案1】:

在我的视图中使用 queryset 后,我​​解决了这个问题。这是我的完整代码:

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

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 2012-12-09
    • 2013-08-02
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2020-12-27
    相关资源
    最近更新 更多