runscript.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. RunScript
  2. =============
  3. :synopsis: Runs a script in the django context.
  4. Introduction
  5. ------------
  6. The runscript command lets you run an arbritrary set of python commands within
  7. the django context. It offers the same usability and functionality as running a
  8. set of commands in shell accessed by::
  9. $ python manage.py shell
  10. Getting Started
  11. ---------------
  12. This example assumes you have followed the tutorial for Django 1.8+, and
  13. created a polls app containing a ``Question`` model. We will create a script
  14. that deletes all of the questions from the database.
  15. To get started create a scripts directory in your project root, next to
  16. manage.py::
  17. $ mkdir scripts
  18. $ touch scripts/__init__.py
  19. Note: The *__init__.py* file is necessary so that the folder is picked up as a
  20. python package.
  21. Next, create a python file with the name of the script you want to run within
  22. the scripts directory::
  23. $ touch scripts/delete_all_questions.py
  24. This file must implement a *run()* function. This is what gets called when you
  25. run the script. You can import any models or other parts of your django project
  26. to use in these scripts.
  27. For example::
  28. # scripts/delete_all_questions.py
  29. from polls.models import Question
  30. def run():
  31. # Fetch all questions
  32. questions = Question.objects.all()
  33. # Delete questions
  34. questions.delete()
  35. Note: You can put a script inside a *scripts* folder in any of your apps too.
  36. Usage
  37. -----
  38. To run any script you use the command *runscript* with the name of the script
  39. that you want to run.
  40. For example::
  41. $ python manage.py runscript delete_all_questions
  42. Note: The command first checks for scripts in your apps i.e. *app_name/scripts*
  43. folder and runs them before checking for and running scripts in the
  44. *project_root/scripts* folder. You can have multiple scripts with the same name
  45. and they will all be run sequentially.
  46. Passing arguments
  47. -----------------
  48. You can pass arguments from the command line to your script by passing a space separated
  49. list of values with ``--script-args``. For example::
  50. $ python manage.py runscript delete_all_questions --script-args staleonly
  51. The list of argument values gets passed as arguments to your *run()* function. For
  52. example::
  53. # scripts/delete_all_questions.py
  54. from datetime import timedelta
  55. from django.utils import timezone
  56. from polls.models import Question
  57. def run(*args):
  58. # Get all questions
  59. questions = Question.objects.all()
  60. if 'staleonly' in args:
  61. # Only get questions more than 100 days old
  62. questions = questions.filter(pub_date__lt=timezone.now() - timedelta(days=100))
  63. # Delete questions
  64. questions.delete()
  65. Debugging
  66. ---------
  67. If an exception occurs you will not get a traceback by default. To get a traceback specify ``--traceback``. For example::
  68. $ python manage.py runscript delete_all_questions --traceback