In a database, i have a field called date. Is there a way to delete a row when the date passes, so that it doesnt show up anymore? Ive tried comparing it to todays date in the view, but this wouldnt happen everyday, and people would still see it on the first page load. Any ideas?
4 Answers
Removing something from your database is not safe for many reasons. Starting from permissions going to on_delete
logic. If you are not sure about that it's totally required to delete something, just mark this row as active=false
.
I would not recomend to use cron
, since it hard to maintain: you have to set different tasks on different environments manually, copy these files somewhere on your VCS, work with bash
instead of python
.
Also, when talking about events, I would not recommend to store something like this in your database, since it is not controlled by VCS and hard to maintain.
If your app is pretty simple schedule is an option.
But if you are looking for some extra info like:
- What rows were deleted?
- Were there any exceptions?
You can move to more complex Celery
with Beat
turned on. Extra dependencies (like Redis
, RabbitMQ
) are the main disadvantage.
Docs:
Related:
-
i looked at scheduler, but that seems to be only for scheduling tasks, like every hour or every 10 minutes. Does it have a feature to do a job at some date? @sobolevn – Abhishek Patel Nov 08 '15 at 14:48
-
brief looking at the source said: sorry, no https://github.com/dbader/schedule/blob/master/schedule/__init__.py – sobolevn Nov 08 '15 at 17:07
-
i think i found my own answer, ive posted it, let me know what you think – Abhishek Patel Nov 08 '15 at 17:16
I believe the best way would be to use a Cron Job or to use a additional conditional in the view to show only rows after the said date.

- 348
- 2
- 11
-
-
The solution user2352738 gives its far better. I don`t know the code you are working on, but since its a display logic (not business logic) you could put a simple *if* in your view and compare dates to show or not the rows. But stick to user2352738 answer and you be better in the long run. – Alan Rezende Nov 08 '15 at 04:22
I would recommend you use a mysql event, since this will run constantly, unlike triggers that are only fired on database operations. You want this to occur outside of anything happening in the application, just based on time, so mysql event will work for this scenario. See full tutorial here: http://www.sitepoint.com/working-with-mysql-events/

- 26
- 1
- 4
I had a easier approach, i guess you could call it "hard-coded". I made a function called deleteevent, which had the following code
def deleteevent():
yesterday = date.today() - timedelta(1)
if Events.objects.filter(event_date = yesterday).count():
Events.objects.filter(event_date = yesterday).delete()
Then, in every other function i had, i called this at the beginning, so the event would be deleted before the page loaded

- 587
- 2
- 8
- 25
-
1Well, I think that is not a good way to do it: for *every* function call you have two extra database hits, moreover you are doing it in the main thread. – sobolevn Nov 08 '15 at 17:20