Search code examples
djangodjango-database

Django - Remove object from database after day


I want to remove object after one day. Here is part of my code:

data = models.DateField(null=True)

So for example value of data field is

10.10.2017

And I want to remove it in

11.10.2017

Thanks for help !


Solution

  • To accomplish something like this you will need to create a periodic task using celery or a cron job that runs every day and removes all the data from the previous day.

    You can use the current date as reference and get old data that is not from today using to_delete_data = ModelName.objects.filter(data__lt=current_date)

    If you want to delete just previous day you will need to use timedelta to get the starting point of the previous day and chain the filters to get the correct data.