~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to docs/howto/custom-management-commands.txt

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-10-12 11:34:35 UTC
  • mfrom: (1.2.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: james.westby@ubuntu.com-20101012113435-9lnsrh0i3mxozbt0
Tags: upstream-1.2.3
ImportĀ upstreamĀ versionĀ 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
.. _howto-custom-management-commands:
2
 
 
3
1
====================================
4
2
Writing custom django-admin commands
5
3
====================================
8
6
 
9
7
Applications can register their own actions with ``manage.py``. For example,
10
8
you might want to add a ``manage.py`` action for a Django app that you're
11
 
distributing. In this document, we will be building a custom ``closepoll`` 
 
9
distributing. In this document, we will be building a custom ``closepoll``
12
10
command for the ``polls`` application from the
13
 
:ref:`tutorial<intro-tutorial01>`.
 
11
:doc:`tutorial</intro/tutorial01>`.
14
12
 
15
13
To do this, just add a ``management/commands`` directory to the application.
16
14
Each Python module in that directory will be auto-discovered and registered as
64
62
 
65
63
                print 'Successfully closed poll "%s"' % poll_id
66
64
 
67
 
The new custom command can be called using ``python manage.py closepoll 
 
65
The new custom command can be called using ``python manage.py closepoll
68
66
<poll_id>``.
69
67
 
70
68
The ``handle()`` method takes zero or more ``poll_ids`` and sets ``poll.opened``
71
69
to ``False`` for each one. If the user referenced any nonexistant polls, a
72
70
:class:`CommandError` is raised. The ``poll.opened`` attribute does not exist
73
 
in the :ref:`tutorial<intro-tutorial01>` and was added to
 
71
in the :doc:`tutorial</intro/tutorial01>` and was added to
74
72
``polls.models.Poll`` for this example.
75
73
 
76
74
The same ``closepoll`` could be easily modified to delete a given poll instead
91
89
            )
92
90
        # ...
93
91
 
94
 
In addition to being able to add custom command line options, all 
95
 
:ref:`management commands<ref-django-admin>` can accept some 
 
92
In addition to being able to add custom command line options, all
 
93
:doc:`management commands</ref/django-admin>` can accept some
96
94
default options such as :djadminopt:`--verbosity` and :djadminopt:`--traceback`.
97
95
 
98
96
Command objects
113
111
Attributes
114
112
----------
115
113
 
116
 
All attributes can be set in your derived class and can be used in 
 
114
All attributes can be set in your derived class and can be used in
117
115
:class:`BaseCommand`'s :ref:`subclasses<ref-basecommand-subclasses>`.
118
116
 
119
117
.. attribute:: BaseCommand.args
133
131
.. attribute:: BaseCommand.help
134
132
 
135
133
  A short description of the command, which will be printed in the
136
 
  help message when the user runs the command 
 
134
  help message when the user runs the command
137
135
  ``python manage.py help <command>``.
138
136
 
139
137
.. attribute:: BaseCommand.option_list
230
228
A command which takes no arguments on the command line.
231
229
 
232
230
Rather than implementing :meth:`~BaseCommand.handle`, subclasses must implement
233
 
:meth:`~NoArgsCommand.handle_noargs`; :meth:`~BaseCommand.handle` itself is 
 
231
:meth:`~NoArgsCommand.handle_noargs`; :meth:`~BaseCommand.handle` itself is
234
232
overridden to ensure no arguments are passed to the command.
235
233
 
236
234
.. method:: NoArgsCommand.handle_noargs(**options)