0

I am still in the development phase of a Django App. Before even writing my views.py, I test them out to see if my models are correctly defined. This I do it in the terminal by invoking

python manage.py shell

But oh so often I make some syntax error prompting me to abort the shell ctrl-D & retype everything. This process is taking forever. It would be better if I could write all this in some file just for my trials & if all's well copy it to views.py.

What's the process for this? Is it as simple as creating a trial.py in my app directory. Won't I have to import the Django env? What's the best way to do this?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • 3
    You're telling me you write every piece of your code in the python interpreter before actually putting it into your application? – Falmarri Nov 25 '10 at 18:24
  • run python manage.py sql app_name – Paulo Scardine Nov 25 '10 at 18:24
  • 1
    "retype everything"? Have you heard of using a "file" to contain your source? The Django tutorial shows a good way to do this. Why aren't you following the Django tutorial? – S.Lott Nov 25 '10 at 21:05

6 Answers6

5

How about writing unit tests? You can execute them easily with one command. You can probably get started by reading the django manual chapter on testing

Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
  • unit tests are not the same as trying functions in shell - imagine, that you know your functions is wrong, but to detect an error you must see results, so, how will you do it with unit tests? – ffriend Nov 25 '10 at 18:37
  • If a test failing doesn't tell you what's not working in your function, then it's not a precise enough test. – Jani Hartikainen Dec 01 '10 at 22:43
2

By all means create a trial.py for simple experimentation, then after doing

python manage.py shell

you can do

>>> import trial

and then invoke the code in trial, directly from the prompt, e.g.

trial.myfunc()

If you need to change things you can just save your changed trial.py and do

reload(trial)

Of course, you may need to recreate any existing objects in the interactive session in order to make use of your changes.

This should be seen as complementary to writing unit tests (as per Jani's answer), but I do find this approach useful for trying things out using iterative refinement.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • 1
    Reloading models doesn't work just like that: http://stackoverflow.com/questions/890924/how-do-you-reload-a-django-model-module-using-the-interactive-interpreter-via-ma/3466579#3466579. – Tomasz Zieliński Nov 25 '10 at 18:52
  • @Tomasz: Agreed, I was thinking of plain Python objects when I mentioned re-creating objects ... your linked-to answer fills in the missing piece. – Vinay Sajip Nov 26 '10 at 11:34
1

Create your file-for-tests in the Django project directory and add path to your project to env variable:

import sys
sys.path.append(os.path.realpath(os.path.dirname(__file__)))

After that you'll be able to import any module from the project (e.g. models from models.py or just functions from views.py) and use your favorite IDE with it's editor and shell.

ffriend
  • 27,562
  • 13
  • 91
  • 132
1

For simpler cases you can use django-extensions add-on that contains shell_plus management command that is similar to the standard shell command but preloads all models.

Tomasz Zieliński
  • 16,136
  • 7
  • 59
  • 83
1

You need to set the module used for settings.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

I also had this problem before,

may be you could install ipython,which has a magic function called like this:

%save.

This will save what you input into a file .

and ipython is a very charming tool ,which can take the palce of standard python prompt perfectly.. It also have other wonderful things !

And in django , if you have install ipython, when you input python manage.py shell,

it will invoke ipython directly.

newlife
  • 778
  • 1
  • 6
  • 20