Django删除除最后五个queryset之外的所有内容 [英] Django Delete all but last five of queryset

查看:112
本文介绍了Django删除除最后五个queryset之外的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个超级简单的django模型:

  class Notification(models.Model):
message = models.TextField()
user = models.ForeignKey(User)
timestamp = models.DateTimeField(default = datetime.datetime.now)

使用ajax,我每分钟检查一下新消息。我只会在任何时候向用户显示最新的五条通知。我想避免的是以下情况。



用户登录并没有通知。当用户的窗口出现时,他会收到10条新消息。既然我只显示他五个,没有什么大不了的。当用户开始删除他的通知时会出现此问题。如果他删除显示的五个,则会在下一个ajax调用或刷新时显示五个较旧的。



我想让我的模型的保存方法删除除了保存新的对象之外的所有东西。不幸的是,你不能使用[5:]来做到这一点。帮助?



编辑



我尝试过没有按预期的方式工作(在模型的保存方法中):

  notes = Notification.objects.filter(user = self.user)[:4] 
Notification.objects.exclude(pk__in = notes).delete()

找到一个奇怪的行为模式,但经过一段时间的测试,它只会删除最新的一个,当一个新的创建。我不知道为什么会这样。在模型的Meta类(按时间戳降序)中处理排序。感谢您的帮助,但我的方式似乎是唯一一个可以一贯工作的人。

解决方案

这有点老了,但是相信您可以执行以下操作:

  notes = Notification.objects.filter(user = self.user)[:4] 
Notification.objects.exclude(pk__in = list(notes))。delete()#list()强制数据库命中。

它花费两个命中率,但避免使用带有事务中间件的for循环。



原因是Django创建一个单独的查询,在Mysql 5.1中,这引发错误

 (1235,这个版本的MySQL还没有支持'LIMIT& IN / ALL / ANY / SOME子查询')

通过使用列表(注释),我们强制查询笔记,避免这个。
这可以进一步优化为:

  notes = Notification.objects.filter(user = self.user)[ :4] .values_list(id,flat = True)#只检索ids。 
Notification.objects.exclude(pk__in = list(notes))。delete()


I have a super simple django model here:

class Notification(models.Model):
    message = models.TextField()
    user = models.ForeignKey(User)
    timestamp = models.DateTimeField(default=datetime.datetime.now)

Using ajax, I check for new messages every minute. I only show the five most recent notifications to the user at any time. What I'm trying to avoid, is the following scenario.

User logs in and has no notifications. While the user's window is up, he receives 10 new messages. Since I'm only showing him five, no big deal. The problem happens when the user starts to delete his notifications. If he deletes the five that are displayed, the five older ones will be displayed on the next ajax call or refresh.

I'd like to have my model's save method delete everything but the 5 most recent objects whenever a new one is saved. Unfortunately, you can't use [5:] to do this. Help?

EDIT

I tried this which didn't work as expected (in the model's save method):

    notes = Notification.objects.filter(user=self.user)[:4]
    Notification.objects.exclude(pk__in=notes).delete()

i couldn't find a pattern in strange behavior, but after a while of testing, it would only delete the most recent one when a new one was created. i have NO idea why this would be. the ordering is taken care of in the model's Meta class (by timestamp descending). thanks for the help, but my way seems to be the only one that works consistently.

解决方案

This is a bit old, but I believe you can do the following:

notes = Notification.objects.filter(user=self.user)[:4]
Notification.objects.exclude(pk__in=list(notes)).delete()  # list() forces a database hit.

It costs two hits, but avoids using the for loop with transactions middleware.

The reason is that Django creates a single query and, in Mysql 5.1, this raises the error

(1235, "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'")

By using list(notes), we force a query of notes, avoiding this. This can be further optimized to:

notes = Notification.objects.filter(user=self.user)[:4].values_list("id", flat=True)  # only retrieve ids.
Notification.objects.exclude(pk__in=list(notes)).delete()

这篇关于Django删除除最后五个queryset之外的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆