在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')
posted @ 2023-04-12 11:18  super_ip  阅读(89)  评论(0编辑  收藏  举报