需求:比如过期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')
标签:10,删除,days,datetime,objects,import,django
From: https://www.cnblogs.com/superip/p/17309198.html