在django中自动删除超过10天的数据
需求:比如过期10天的产品自动删除记录
posting_date = models.DateTimeField(auto_now_add=True)
# purge_old_data.py
from django.core.management.base import BaseCommand, CommandError
from cus_leads.models import CustomerLeads
from datetime import datetime, timedelta
class Command(BaseCommand):
help = 'Delete objects older than 10 days'
def handle(self, *args, **options):
CustomerLeads.objects.filter(posting_date__lte=datetime.now()-timedelta(days=10)).delete()
self.stdout.write('Deleted objects older than 10 days')
本文来自博客园,作者:super_ip,转载请注明原文链接:https://www.cnblogs.com/superip/p/17309198.html