django “如何”系列2:如何编写django-admin 命令

应用可以使用manage.py注册自己的动作,例如,你可能想要为你即将发布的应用添加一个manage.py 操作。这节我们将为polls应用添加一个closepoll的命令

添加一个management/commands目录如下

polls/
    __init__.py
    models.py
    management/
        __init__.py
        commands/
            __init__.py
            _private.py
            closepoll.py
    tests.py
    views.py

这样添加之后,任何包含polls在INSTALLED_APPS的项目都可以使用closepoll命令

_private.py模块不会作为一个管理命令

closepoll.py模块有一个要求--必须定义一个类Command拓展或者继承BaseCommand

下面是这个命令的一个实例实现(closepoll.py)

from django.core.management.base import BaseCommand, CommandError
from example.polls.models import Poll

class Command(BaseCommand):
    args = ‘<poll_id poll_id ...>‘
    help = ‘Closes the specified poll for voting‘

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

            poll.opened = False
            poll.save()

            self.stdout.write(‘Successfully closed poll "%s"\n‘ % poll_id)

注意:使用self.stdout和self.stderr而不是stdout和stderr

这个新命令的可以通过python manage.py closepoll <poll_id>来调用

更多更详细的关于BaseCommand的代码附于下面:常用到的参数是args(参数格式说明)和help(命令说明),必须实现的方法是handle(如何实现这个命令),正如上面的例子的那样,如果没有特别的要求,下面的代码其实不用看了。

class BaseCommand(object):
    """
    The base class from which all management commands ultimately
    derive.

    Use this class if you want access to all of the mechanisms which
    parse the command-line arguments and work out what code to call in
    response; if you don‘t need to change any of that behavior,
    consider using one of the subclasses defined in this file.

    If you are interested in overriding/customizing various aspects of
    the command-parsing and -execution behavior, the normal flow works
    as follows:

    1. ``django-admin.py`` or ``manage.py`` loads the command class
       and calls its ``run_from_argv()`` method.

    2. The ``run_from_argv()`` method calls ``create_parser()`` to get
       an ``OptionParser`` for the arguments, parses them, performs
       any environment changes requested by options like
       ``pythonpath``, and then calls the ``execute()`` method,
       passing the parsed arguments.

    3. The ``execute()`` method attempts to carry out the command by
       calling the ``handle()`` method with the parsed arguments; any
       output produced by ``handle()`` will be printed to standard
       output and, if the command is intended to produce a block of
       SQL statements, will be wrapped in ``BEGIN`` and ``COMMIT``.

    4. If ``handle()`` raised a ``CommandError``, ``execute()`` will
       instead print an error message to ``stderr``.

    Thus, the ``handle()`` method is typically the starting point for
    subclasses; many built-in commands and command types either place
    all of their logic in ``handle()``, or perform some additional
    parsing work in ``handle()`` and then delegate from it to more
    specialized methods as needed.

    Several attributes affect behavior at various steps along the way:

    ``args``
        A string listing the arguments accepted by the command,
        suitable for use in help messages; e.g., a command which takes
        a list of application names might set this to ‘<appname
        appname ...>‘.

    ``can_import_settings``
        A boolean indicating whether the command needs to be able to
        import Django settings; if ``True``, ``execute()`` will verify
        that this is possible before proceeding. Default value is
        ``True``.

    ``help``
        A short description of the command, which will be printed in
        help messages.

    ``option_list``
        This is the list of ``optparse`` options which will be fed
        into the command‘s ``OptionParser`` for parsing arguments.

    ``output_transaction``
        A boolean indicating whether the command outputs SQL
        statements; if ``True``, the output will automatically be
        wrapped with ``BEGIN;`` and ``COMMIT;``. Default value is
        ``False``.

    ``requires_model_validation``
        A boolean; if ``True``, validation of installed models will be
        performed prior to executing the command. Default value is
        ``True``. To validate an individual application‘s models
        rather than all applications‘ models, call
        ``self.validate(app)`` from ``handle()``, where ``app`` is the
        application‘s Python module.

    """
    # Metadata about this command.
    option_list = (
        make_option(‘-v‘, ‘--verbosity‘, action=‘store‘, dest=‘verbosity‘, default=‘1‘,
            type=‘choice‘, choices=[‘0‘, ‘1‘, ‘2‘, ‘3‘],
            help=‘Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output‘),
        make_option(‘--settings‘,
            help=‘The Python path to a settings module, e.g. "myproject.settings.main". If this isn\‘t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.‘),
        make_option(‘--pythonpath‘,
            help=‘A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".‘),
        make_option(‘--traceback‘, action=‘store_true‘,
            help=‘Print traceback on exception‘),
    )
    help = ‘‘
    args = ‘‘

    # Configuration shortcuts that alter various logic.
    can_import_settings = True
    requires_model_validation = True
    output_transaction = False  # Whether to wrap the output in a "BEGIN; COMMIT;"

    def __init__(self):
        self.style = color_style()

    def get_version(self):
        """
        Return the Django version, which should be correct for all
        built-in Django commands. User-supplied commands should
        override this method.

        """
        return django.get_version()

    def usage(self, subcommand):
        """
        Return a brief description of how to use this command, by
        default from the attribute ``self.help``.

        """
        usage = ‘%%prog %s [options] %s‘ % (subcommand, self.args)
        if self.help:
            return ‘%s\n\n%s‘ % (usage, self.help)
        else:
            return usage

    def create_parser(self, prog_name, subcommand):
        """
        Create and return the ``OptionParser`` which will be used to
        parse the arguments to this command.

        """
        return OptionParser(prog=prog_name,
                            usage=self.usage(subcommand),
                            version=self.get_version(),
                            option_list=self.option_list)

    def print_help(self, prog_name, subcommand):
        """
        Print the help message for this command, derived from
        ``self.usage()``.

        """
        parser = self.create_parser(prog_name, subcommand)
        parser.print_help()

    def run_from_argv(self, argv):
        """
        Set up any environment changes requested (e.g., Python path
        and Django settings), then run this command.

        """
        parser = self.create_parser(argv[0], argv[1])
        options, args = parser.parse_args(argv[2:])
        handle_default_options(options)
        self.execute(*args, **options.__dict__)

    def execute(self, *args, **options):
        """
        Try to execute this command, performing model validation if
        needed (as controlled by the attribute
        ``self.requires_model_validation``). If the command raises a
        ``CommandError``, intercept it and print it sensibly to
        stderr.
        """
        show_traceback = options.get(‘traceback‘, False)

        # Switch to English, because django-admin.py creates database content
        # like permissions, and those shouldn‘t contain any translations.
        # But only do this if we can assume we have a working settings file,
        # because django.utils.translation requires settings.
        saved_lang = None
        if self.can_import_settings:
            try:
                from django.utils import translation
                saved_lang = translation.get_language()
                translation.activate(‘en-us‘)
            except ImportError, e:
                # If settings should be available, but aren‘t,
                # raise the error and quit.
                if show_traceback:
                    traceback.print_exc()
                else:
                    sys.stderr.write(smart_str(self.style.ERROR(‘Error: %s\n‘ % e)))
                sys.exit(1)

        try:
            self.stdout = options.get(‘stdout‘, sys.stdout)
            self.stderr = options.get(‘stderr‘, sys.stderr)
            if self.requires_model_validation:
                self.validate()
            output = self.handle(*args, **options)
            if output:
                if self.output_transaction:
                    # This needs to be imported here, because it relies on
                    # settings.
                    from django.db import connections, DEFAULT_DB_ALIAS
                    connection = connections[options.get(‘database‘, DEFAULT_DB_ALIAS)]
                    if connection.ops.start_transaction_sql():
                        self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()) + ‘\n‘)
                self.stdout.write(output)
                if self.output_transaction:
                    self.stdout.write(‘\n‘ + self.style.SQL_KEYWORD("COMMIT;") + ‘\n‘)
        except CommandError, e:
            if show_traceback:
                traceback.print_exc()
            else:
                self.stderr.write(smart_str(self.style.ERROR(‘Error: %s\n‘ % e)))
            sys.exit(1)
        if saved_lang is not None:
            translation.activate(saved_lang)

    def validate(self, app=None, display_num_errors=False):
        """
        Validates the given app, raising CommandError for any errors.

        If app is None, then this will validate all installed apps.

        """
        from django.core.management.validation import get_validation_errors
        try:
            from cStringIO import StringIO
        except ImportError:
            from StringIO import StringIO
        s = StringIO()
        num_errors = get_validation_errors(s, app)
        if num_errors:
            s.seek(0)
            error_text = s.read()
            raise CommandError("One or more models did not validate:\n%s" % error_text)
        if display_num_errors:
            self.stdout.write("%s error%s found\n" % (num_errors, num_errors != 1 and ‘s‘ or ‘‘))

    def handle(self, *args, **options):
        """
        The actual logic of the command. Subclasses must implement
        this method.

        """
        raise NotImplementedError()

BaseCommand

时间: 2024-10-21 05:20:28

django “如何”系列2:如何编写django-admin 命令的相关文章

django “如何”系列3:如何编写模型域(model filed)

django自带很多的域类--CharField,DateField等等--,如果django的这些域都不能满足你精确的要求,那么你可以编写自己的模型域. django自带的域没有和数据库列类型一一对应的,只有简单的VARCHAR,INTEGER等类型,为了使用更复杂的类型,例如多边形,你可以定义你的域子类:或者,你可能有一个很复杂的对象,这个对象可以通过某些方法序列化标准的数据库列类型,那么你可以定义自己的域子类. 示例对象 创建自定义的域需要注意一些特别的问题,为了使问题更简单的得以阐述,我

django “如何”系列4:如何编写自定义模板标签和过滤器

django的模板系统自带了一系列的内建标签和过滤器,一般情况下可以满足你的要求,如果觉得需更精准的模板标签或者过滤器,你可以自己编写模板标签和过滤器,然后使用{% load %}标签使用他们. 代码布局 自定义标签和过滤器必须依赖于一个django app,也就是说,自定义标签和过滤器是绑定app的.该app应该包含一个templatetags目录,这个目录一个和model.py,views.py在同一个层级,记得在该目录下建立一个__init__.py文件一遍django知道这是一个pyth

django “如何”系列5:如何编写自定义存储系统

如果你需要提供一个自定义的文件存储-一个常见的例子便是在远程系统上存储文件-你可以通过定义一个自己的存储类来做这件事情,你将通过一下步骤: 你自定义的存储系统一定是django.core.files.storage.Storage的子类 from django.core.files.storage import Storage class MyStorage(Storage): ... django必须可以实例化你的存储系统(不使用任何参数),这意味着任何的设置都必须从django.conf.s

Python+Django+SAE系列教程16-----cookie&amp;session

本章我们来讲解cookie和session ,这两个东西相信大家一定不陌生,概念就不多讲了,我们直接来看其用法,首先是cookie,我们在view中添加三个视图,一个是显示cookie的,一个是设置cookie的,如下: def show_cookie(request): if "MyTestCookie" in request.COOKIES: return HttpResponse("Cookie[MyTestCookie]的内容是: %s" % request

Python+Django+SAE系列教程11-----request/pose/get/表单

表单request,post,get 首先我们来看看Request对象,在这个对象中包含了一些有用的信息,学过B/S开发的人来说这并不陌生,我们来看看在Django中是如何实现的: 属性/方法 说明 举例 request.path 除域名以外的请求路径,以正斜杠开头 "/hello/" request.get_host() 主机名(比如,通常所说的域名) "127.0.0.1:8000" or"www.example.com" request.g

Python+Django+SAE系列教程17-----authauth (认证与授权)系统1

通过session,我们可以在多次浏览器请求中保持数据,接下来的部分就是用session来处理用户登录了. 当然,不能仅凭用户的一面之词,我们就相信,所以我们需要认证. 当然了,Django 也提供了工具来处理这样的常见任务(就像其他常见任务一样). Django 用户认证系统处理用户帐号,组,权限以及基于cookie的用户会话.这个系统一般被称为 auth/auth (认证与授权)系统. 这个系统的名称同时也表明了用户常见的两步处理. 我们需要: 1.     验证 (认证) 用户是否是他所宣

Python+Django+SAE系列教程12-----配置MySQL数据库

因为SAE上支持的是Mysql,首先我们要在本地配置一个Mysql的环境 ,我在网上找到MySQL-python-1.2.4b4.win32-py2.7.exe,并双击 安装 选择典型安装 安装结束后,会弹出配置数据库的界面 : 然后输数据管理员密码: 最后是运行服务. 这个过程并不复杂,安装完成Mysql以后,系统会启动数据库服务,由于Mysql是用命令行控制的,想我这样懒的 人还是需要借助一个可视化 工具来管理,我选择MySQL-Front. 在网上找到MySQL_Front_Setup.1

Python+Django+SAE系列教程13-----MySQL记录的添\删\改

建立了数据库后,我们就来做一个简单的表(person_classroom)的添加.删除.修改的操作. 首先我们建立一个添加的页面的模板Classroom_Add.html(添加的表单)并把它放在Bidding\templates\person中: Classroom_Add.html: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>数据库操作

Django:创建用户模型报错: (admin.E108) The value of &#39;list_display[4]&#39;解决方案

参考资料:虫师-<web接口开发与自动化测试:基于python语言> 日常学习Django框架中,创建了用户模型,但是页面功能验证时候,提示不能进行列表字段操作,debug好久,才找到问题原因,心累... 下面是大概过程和解决方案... models.py文件代码: 1 from django.db import models 2 3 # Create your models here. 4 # 发布会表 5 class Event(models.Model): 6 name = models

04-在views.py中使用class编写django项目

在views.py中使用class编写django项目 知识点:urls.py配置文件本质是URL与要为该URL调用的视图函数之间的映射表 1. 用户信息展示页 1.1 在urls.py中添加用户展示页路由 from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^user_list/', views.UserList.as_vi