I have a model named 'Complaint' with numerous entries and I want to delete the entry if it is in database for more than 3 days. How can I do that is there any query for specifying the time. I'm also storing the time at which the entry was stored. This is my models.py file.
complaint_for = models.CharField(max_length=25, choices=COMPLAINT_CHOICES, default='Misbehave')
name = models.CharField(max_length=50)
created = models.DateTimeField(auto_now_add=True)
To be honest, I have never worked worked with Django, but as far as I know, a good solution to delete records after a certain time interval, is to run automatically a database script. In your case, I can imagine that you have somewhere in your db-table a column which is named creation_time
or something like that and then with your script you can just check if the difference between the current time and the creation_time
is more or equal to the specified time interval. If so, the script deletes the record. In order to run such a script automatically, you can start a cron job
. You can read more on the topic how to delete records with sql here and here on the topic cron jobs.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments