Advertisement
object_254

custom django-admin commands

Dec 10th, 2021
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. from django.core.management.base import BaseCommand, CommandError
  2. from polls.models import Question as Poll
  3.  
  4. class Command(BaseCommand):
  5.     help = 'Closes the specified poll for voting'
  6.  
  7.     def add_arguments(self, parser):
  8.         parser.add_argument('poll_ids', nargs='+', type=int)
  9.  
  10.     def handle(self, *args, **options):
  11.         for poll_id in options['poll_ids']:
  12.             try:
  13.                 poll = Poll.objects.get(pk=poll_id)
  14.             except Poll.DoesNotExist:
  15.                 raise CommandError('Poll "%s" does not exist' % poll_id)
  16.  
  17.             poll.opened = False
  18.             poll.save()
  19.  
  20.             self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement