Computer scienceBackendDjangoDjango fixes

Custom management commands

Theory

Time's up for old digital memories

Report a typo
Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!
How would you create a Django management command to remove expired sessions older than 30 days and display the number of deleted sessions?
Reorder lines using drag or arrows. Adjust indentation with left buttons
                from django.contrib.sessions.models import Session
              
                thirty_days_ago = timezone.now() - timezone.timedelta(days=30)
              
                from django.utils import timezone
              
                class Command(BaseCommand):
              
                deleted_count = Session.objects.filter(expire_date__lt=thirty_days_ago).delete()[0]
              
                def handle(self, *args, **options):
              
                from django.core.management.base import BaseCommand
              
                self.stdout.write(f"Deleted {deleted_count} expired sessions.\n")
              

Create a free account to access the full topic