Opened 8 years ago

Closed 8 years ago

Last modified 3 years ago

#25680 closed New feature (fixed)

Add support for manage.py shell -e 'run_some_code_as_django()'

Reported by: Håkan W Owned by: Niels Van Och
Component: Core (Management commands) Version: 1.8
Severity: Normal Keywords:
Cc: markus.magnuson@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm noticing a trend in my projects that I tend to create management commands that are just wrappers around a simple call that needs to be executed as Django.

Sometimes it would be so much easier to be able to just do this (from Fabric, or a bash script):

python manage.py shell -e 'do_something()'

Attachments (1)

add_command_option_to_shell.diff.txt (2.6 KB ) - added by Niels Van Och 8 years ago.

Download all attachments as: .zip

Change History (19)

comment:1 by Markus Amalthea Magnuson, 8 years ago

Cc: markus.magnuson@… added

comment:2 by Collin Anderson, 8 years ago

I would personally use this pretty frequently.

comment:3 by Adam Johnson, 8 years ago

Did you know? You can already do (at least in bash and zsh):

python manage.py shell <<< 'do_something()'

This sends the string in on stdin which shell simply interprets the same as typed commands. However it should be noted you can't do this in some shells or from e.g. subprocess.Popen without shell=True.

Also, if this does get implemented I'd suggest the -c flag to mirror python -c.

comment:4 by Niels Van Och, 8 years ago

Owner: changed from nobody to Niels Van Och
Status: newassigned

comment:5 by Tim Graham, 8 years ago

Component: UncategorizedCore (Management commands)

I'm not convinced about accepting the ticket given the workaround Adam noted.

I think something like python -c "import django; django.setup(); ..." is also equivalent.

comment:6 by Niels Van Och, 8 years ago

I started on a patch for this, I'll finish it up. In my head the feature has some merit.

comment:7 by Niels Van Och, 8 years ago

Has patch: set

by Niels Van Och, 8 years ago

comment:8 by Tim Graham, 8 years ago

Adam's suggestion isn't completely equivalent as it will output the Python prompt too:

$ python manage.py shell <<< 'print(1)'
Python 3.5.0 (default, Sep 14 2015, 10:39:54) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 1
>>> 

The discussion about deprecating the shell command in #19737 (ultimately closed as wontfix) also left me wondering if we should add more options to it.

I created a django-developers thread to get more feedback.

in reply to:  8 comment:9 by Shai Berger, 8 years ago

Replying to timgraham:

Adam's suggestion isn't completely equivalent as it will output the Python prompt too:

$ python manage.py shell <<< 'print(1)'
Python 3.5.0 (default, Sep 14 2015, 10:39:54) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 1
>>> 

More importantly, it "confiscates" stdin, so the called function cannot interact with the user:

$ python <<< 'print input()'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
EOFError: EOF when reading a line

comment:10 by Adam Johnson, 8 years ago

Indeed it does confiscate. But interestingly python alone doesn't output the prompt:

$ python <<< 'print(23 * 7)'
161

So manage.py shell can maybe do something to avoid outputting the prompt in this case - which is still separate to the case of a flag of course.

in reply to:  10 comment:11 by Simon Charette, 8 years ago

Replying to adamchainz:

Indeed it does confiscate. But interestingly python alone doesn't output the prompt:

$ python <<< 'print(23 * 7)'
161

So manage.py shell can maybe do something to avoid outputting the prompt in this case - which is still separate to the case of a flag of course.

In the long run it might just be easier to add this flag since we might have to also special case ipython and bpython also and from my understanding the <<< trick won't work on Windows.

comment:12 by Hugo Osvaldo Barrera, 8 years ago

Why not just

import django
django.setup()

more_code_here

comment:13 by Collin Anderson, 8 years ago

For me personally, I have a bunch of code in my ./manage.py that setups up the correct virtual environment so for me it takes a little more than django.setup().

comment:14 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

No strong opposition to the feature request has appeared so far.

comment:15 by Tim Graham, 8 years ago

Patch needs improvement: set
Summary: Feature request: support python manage.py shell -e 'run_some_code_as_django()'Add support for manage.py shell -e 'run_some_code_as_django()'

Left comments for improvement on the pull request.

Last edited 8 years ago by Tim Graham (previous) (diff)

comment:16 by Niels Van Och, 8 years ago

Patch needs improvement: unset

Resolved the comments, except one, which I think can't be fixed.

comment:17 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 7f7553dd:

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.

comment:18 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In d26d1c1:

Refs #25680 -- Added shell tests for globals available in passed commands.

Note: See TracTickets for help on using tickets.
Back to Top