Cannot update a query once a slice has been taken

后端 未结 6 722
南笙
南笙 2020-12-09 02:51

I am trying to do this:

UserLog.objects.filter(user=user).filter(action=\'message\').filter(timestamp__lt=now)[0:5].update(read=True)

but I

6条回答
  •  我在风中等你
    2020-12-09 03:29

    I was getting the same error when attempting to limit the number of records returned by a queryset.

    I found that if we're using one of Django's class-based generic views such as the ArchiveIndexView, we can use the paginate_by = attribute to limit the number of records.

    For example (in views.py):

    from django.views.generic import ArchiveIndexView
    from .models import Entry
    
    class HomeListView(ArchiveIndexView):
        """ Blog Homepage """
        model = Entry
        date_field = 'pub_date' 
        template_name = 'appname/home.html'
        queryset = Entry.objects.filter(
            is_active=True).order_by('-pub_date', 'title')
        paginate_by = 30
    

提交回复
热议问题