这有点旧,但是我相信您可以执行以下 *** 作:
notes = Notification.objects.filter(user=self.user)[:4]Notification.objects.exclude(pk__in=list(notes)).delete() # list() forces a database hit.
它花费了两笔费用,但避免将for循环与事务中间件一起使用。
使用的原因
list(notes)是Django创建了一个不带查询的查询,而在MySQL 5.1中,这引发了错误
(1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")
通过使用
list(notes),我们强制查询
notes,避免了这种情况。可以进一步优化以:
notes = Notification.objects.filter(user=self.user)[:4].values_list("id", flat=True) # only retrieve ids.Notification.objects.exclude(pk__in=list(notes)).delete()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)