Created
September 23, 2021 21:03
-
-
Save Marceloromeugoncalves/247b0ba38cb2c158393fb1705c9d5202 to your computer and use it in GitHub Desktop.
Exemplo de criação de um command com Django, para ser executado com o comando python manage.py.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
polls/ | |
__init__.py | |
models.py | |
management/ | |
__init__.py | |
commands/ | |
__init__.py | |
_private.py | |
closepoll.py | |
tests.py | |
views.py | |
""" | |
#polls/management/commands/closepoll.py | |
from django.core.management.base import BaseCommand | |
from django.core.management.base import CommandError | |
from polls.models import Question as Poll | |
class Command(BaseCommand): | |
help = 'Closes the specified poll for voting' | |
def add_arguments(self, parser): | |
parser.add_argument('poll_id', nargs='+', type=int) | |
def handle(self, *args, **kwargs): | |
for poll_id in options['poll_ids']: | |
try: | |
poll = Poll.objects.get(pk=poll_id) | |
except Poll.DoesNotExist: | |
reiase CommandError('Poll "%s" does not exists' % poll_id) | |
poll.opened = False | |
poll.save() | |
self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment