“Verwaltungsbefehle Django” Code-Antworten

Verwaltungsbefehle Django

from django.core.management.base import BaseCommand, 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_ids', nargs='+', type=int)

    def handle(self, *args, **options):
        for poll_id in options['poll_ids']:
            try:
                poll = Poll.objects.get(pk=poll_id)
            except Poll.DoesNotExist:
                raise CommandError('Poll "%s" does not exist' % poll_id)

            poll.opened = False
            poll.save()

            self.stdout.write(self.style.SUCCESS('Successfully closed poll "%s"' % poll_id))
Energetic Echidna

Django Run Management -Befehl aus Code

from django.core.management import call_command

call_command('my_command', 'foo', bar='baz')
Alive Armadillo

Verwaltungskommando in Django

from django.core.management.base import BaseCommand
from django.utils import timezone

class Command(BaseCommand):
    help = 'Displays current time'

    def handle(self, *args, **kwargs):
        time = timezone.now().strftime('%X')
        self.stdout.write("It's now %s" % time)
Motionless Magpie

Ähnliche Antworten wie “Verwaltungsbefehle Django”

Fragen ähnlich wie “Verwaltungsbefehle Django”

Weitere verwandte Antworten zu “Verwaltungsbefehle Django” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen