Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

management commands 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))
Comment

django run management command from code

from django.core.management import call_command

call_command('my_command', 'foo', bar='baz')
Comment

management command 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)
Comment

PREVIOUS NEXT
Code Example
Python :: pyqt menubar example 
Python :: python 3 f string float format 
Python :: copy file python 
Python :: convert list to numpy array 
Python :: levenshtein distance 
Python :: tensorflow keras load model 
Python :: how to get the author on discord.py 
Python :: how to count number of columns in dataframe python 
Python :: add a button pyqt5 
Python :: how to convert boolean type list to integer 
Python :: cross join pandas 
Python :: python dict for k v 
Python :: python multiline comment 
Python :: how to install packages inside thepython script 
Python :: xml to excel python 
Python :: numpy remove columns containing nan 
Python :: python dictionary get 
Python :: get index of value in list if value meet condition python 
Python :: python partial 
Python :: django install 
Python :: python array append 
Python :: python dict get random key 
Python :: subtract from dataframe column 
Python :: python split paragraph 
Python :: who created python 
Python :: code for python shell 3.8.5 games 
Python :: UTC to ISO 8601 with TimeZone information (Python 3): 
Python :: selenium select element by id 
Python :: python multiplication array 
Python :: split a string into N equal parts. 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =