mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #25680 -- Added django-admin shell --command option.
Add a -c option to the shell command to execute a command passed as a string as Django.
This commit is contained in:
parent
0cc32a8f97
commit
7f7553dd30
@ -18,6 +18,8 @@ class Command(BaseCommand):
|
|||||||
help='When using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.')
|
help='When using plain Python, ignore the PYTHONSTARTUP environment variable and ~/.pythonrc.py script.')
|
||||||
parser.add_argument('-i', '--interface', choices=self.shells, dest='interface',
|
parser.add_argument('-i', '--interface', choices=self.shells, dest='interface',
|
||||||
help='Specify an interactive interpreter interface. Available options: "ipython", "bpython", and "python"')
|
help='Specify an interactive interpreter interface. Available options: "ipython", "bpython", and "python"')
|
||||||
|
parser.add_argument('-c', '--command', dest='command',
|
||||||
|
help='Instead of opening an interactive shell, run a command as Django and exit.')
|
||||||
|
|
||||||
def _ipython_pre_011(self):
|
def _ipython_pre_011(self):
|
||||||
"""Start IPython pre-0.11"""
|
"""Start IPython pre-0.11"""
|
||||||
@ -93,6 +95,11 @@ class Command(BaseCommand):
|
|||||||
)
|
)
|
||||||
options['interface'] = 'python'
|
options['interface'] = 'python'
|
||||||
|
|
||||||
|
# Execute the command and exit.
|
||||||
|
if options['command']:
|
||||||
|
exec(options['command'])
|
||||||
|
return
|
||||||
|
|
||||||
available_shells = [options['interface']] if options['interface'] else self.shells
|
available_shells = [options['interface']] if options['interface'] else self.shells
|
||||||
|
|
||||||
for shell in available_shells:
|
for shell in available_shells:
|
||||||
|
@ -970,6 +970,15 @@ behavior you can use the ``--no-startup`` option. e.g.::
|
|||||||
|
|
||||||
django-admin shell --interface python --no-startup
|
django-admin shell --interface python --no-startup
|
||||||
|
|
||||||
|
.. django-admin-option:: --command, -c <command>
|
||||||
|
|
||||||
|
.. versionadded:: 1.10
|
||||||
|
|
||||||
|
The ``--command`` option lets you pass a command as a string to execute it as
|
||||||
|
Django, like so::
|
||||||
|
|
||||||
|
django-admin shell --command="import django; print(django.__version__)"
|
||||||
|
|
||||||
showmigrations [<app_label> [<app_label>]]
|
showmigrations [<app_label> [<app_label>]]
|
||||||
------------------------------------------
|
------------------------------------------
|
||||||
|
|
||||||
|
@ -224,6 +224,9 @@ Management Commands
|
|||||||
* The :djadmin:`shell` ``--interface`` option now accepts ``python`` to force
|
* The :djadmin:`shell` ``--interface`` option now accepts ``python`` to force
|
||||||
use of the "plain" Python interpreter.
|
use of the "plain" Python interpreter.
|
||||||
|
|
||||||
|
* The new :djadminopt:`shell --command <--command>` option lets you run a
|
||||||
|
command as Django and exit, instead of opening the interactive shell.
|
||||||
|
|
||||||
Migrations
|
Migrations
|
||||||
^^^^^^^^^^
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
0
tests/shell/__init__.py
Normal file
0
tests/shell/__init__.py
Normal file
19
tests/shell/tests.py
Normal file
19
tests/shell/tests.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from django import __version__
|
||||||
|
from django.core.management import call_command
|
||||||
|
from django.test import SimpleTestCase
|
||||||
|
from django.test.utils import patch_logger
|
||||||
|
|
||||||
|
|
||||||
|
class ShellCommandTestCase(SimpleTestCase):
|
||||||
|
|
||||||
|
def test_command_option(self):
|
||||||
|
with patch_logger('test', 'info') as logger:
|
||||||
|
call_command(
|
||||||
|
'shell',
|
||||||
|
command=(
|
||||||
|
'import django; from logging import getLogger; '
|
||||||
|
'getLogger("test").info(django.__version__)'
|
||||||
|
),
|
||||||
|
)
|
||||||
|
self.assertEqual(len(logger), 1)
|
||||||
|
self.assertEqual(logger[0], __version__)
|
Loading…
Reference in New Issue
Block a user