倒数后如何删除Django对象? [英] How to delete Django object after countdown?
问题描述
在我正在使用的平台上,我们希望用户能够为其创建的对象设置到期时间.他们设置的倒计时结束后,应删除该对象.
In the platform I am working on we want users to be able to set expiry times on the objects they create. After the countdown they set expires, that object should be deleted.
您会如何建议这样做?
我要澄清一下,每个对象的到期时间会有所不同.
I should clarify that the expiry time will vary per object.
推荐答案
执行所描述的最常见的方法是创建一个存储创建时间的列,该列存储到期时间,并简单地考虑过期后删除的项目.
The most common way to do what you are describing is to create a column that stores the time of creation and column for the expiry time and simply consider items deleted after the expiry time.
您 必须在针对该表的任何查询中都包含一个条件,以便在您打算检索未过期项目列表时过滤出过期项目.
You will have to include a condition in any queries against that table to filter out expired items whenever you intend to retrieve a list of non-expired items.
如果由于某种原因(例如出于监管目的或出于业务要求)而删除数据很重要,则可以设置定期任务(例如cron作业)以删除所有过期的记录.这可以(并且经常)与我上面描述的方法结合使用,这样您就可以确信,即使自上次运行周期性任务以来某些对象已过期,用户仍可以看到正确的数据.
If it is important to eliminate the data for some reason (like a regulatory one or because it's a business requirement) you can set up a periodic task, like a cron job, to delete any records which are expired. This could be (and often is) used in conjunction with the approach I described above, so that you can be confident users are seeing correct data even if some objects have expired since the last time the periodic task was run.
这篇关于倒数后如何删除Django对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!