django add custom commands to manage py

Solutions on MaxInterview for django add custom commands to manage py by the best coders in the world

showing results for - "django add custom commands to manage py"
Celian
24 Jan 2016
1# To implement the command, edit polls/management/commands/closepoll.py to look like this:
2from django.core.management.base import BaseCommand, CommandError
3from polls.models import Question as Poll
4
5class Command(BaseCommand):
6    help = 'Closes the specified poll for voting'
7
8    def add_arguments(self, parser):
9        parser.add_argument('poll_ids', nargs='+', type=int)
10
11    def handle(self, *args, **options):
12        for poll_id in options['poll_ids']:
13            try:
14                poll = Poll.objects.get(pk=poll_id)
15            except Poll.DoesNotExist:
16                raise CommandError('Poll "%s" does not exist' % poll_id)
17
18            poll.opened = False
19            poll.save()
20
21            self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))
22
23 # polls/
24 #   __init__.py
25 #   models.py
26 #   management/
27 #       commands/
28 #           _private.py
29 #           closepoll.py
30 #   tests.py
31 #   views.py