![]() Or python manage.py create_users 2 -a Arbitrary List of Arguments Usage: python manage.py create_users 2 -admin create_user ( username = username, email = '', password = '123' ) create_superuser ( username = username, email = '', password = '123' ) else : User. format ( prefix = prefix, random_string = get_random_string ()) else : username = get_random_string () if admin : User. add_argument ( '-p', '-prefix', type = str, help = 'Define a username prefix', ) def handle ( self, * args, ** kwargs ): total = kwargs prefix = kwargs for i in range ( total ): if prefix : username = ''. add_argument ( 'total', type = int, help = 'Indicates the number of users to be created' ) # Optional argument parser. Management/commands/create_users.py from import User from import BaseCommand from import get_random_string class Command ( BaseCommand ): help = 'Create random users' def add_arguments ( self, parser ): parser. In the example below you will find the definition ofĪn argument named “prefix”, which will be used to compose the username field: The optional (and named) arguments can be passed in any order. Here is how one would use it: python manage.py create_users 10 Optional Arguments create_user ( username = get_random_string (), email = '', password = '123' ) add_argument ( 'total', type = int, help = 'Indicates the number of users to be created' ) def handle ( self, * args, ** kwargs ): total = kwargs for i in range ( total ): User. Will define the number of users that will be created by the command. It takes a mandatory argument named total, which The next example is a command that create random user instances. To handle arguments in our custom command we should define a method named add_arguments. Queries to the database using Django’s ORM and interact with all your project’s resources.ĭjango make use of the argparse, which is part of Python’s standard That means you can import models, execute Main advantage is that all Django machinery is loaded and ready to be used. ![]() You may be asking yourself, how is that different from a regular Python script, or what’s the benefit of it. This command can be executed as: python manage.py what_time_is_it See how we named our module what_time_is_it.py. TheĬommand code should be defined inside the handle() method. write ( "It's now % s" % time )īasically a Django management command is composed by a class named Command which inherits from BaseCommand. Management/commands/what_time_is_it.py from import BaseCommand from django.utils import timezone class Command ( BaseCommand ): help = 'Displays current time' def handle ( self, * args, ** kwargs ): time = timezone. For example, if our command wasĬalled my_custom_command.py, then we will be able to execute it via: python manage.py my_custom_commandīelow, a basic example of what the custom command should look like: The name of the command file will be used to invoke using the command line utility. | | +- my_custom_command.py <- module where command is going to live We can create our own commands for our apps and include them in the list by creating a management/commandsĭirectory inside an app directory, like below: mysite/ <- project directory Output: Type 'manage.py help ' for help on a specific subcommand. You can run the command below: python manage.py help You are probablyĪlready familiar with commands like startproject, runserver or collectstatic. Just before we get started, let’s take a moment to familiarize with Django’s command line interface. In this tutorial you are going to learn how to code your own commands. ![]() Those managementĬommands can be very handy when you need to interact with your application via command line using a terminal and it canĪlso serve as an interface to execute cron jobs. ![]() A nice thing about it is that you can also add your own commands. Django comes with a variety of command line utilities that can be either invoked using django-admin.py or theĬonvenient manage.py script. ![]()
0 Comments
Leave a Reply. |