0

The project that I'm recently working on load initial data by running a script:

python manage.py shell < add_initial_data.py

I'm making functional tests now and have to call this command to load initial data in the test database.

I'm trying to use subprocess.call and call_command to run that script but I can't find the option to redirect the script file to shell.

Is there a way to do that?

I've tried with

subprocess.call(['python', 'manage.py', 'shell', '<', 'add_initial_data.py'])

and

call_command('shell', '<', 'add_initial_data.py')

but gives error not recognizing < add_initial_data.py

Caco
  • 1,601
  • 1
  • 26
  • 53

1 Answers1

0

I think you are approaching this from wrong angle. Django uses fixtures to load initial data. It is also supported in test cases django test fixture loading If you are in python 3 and insist on your approach you might do

./manage.py shell <<EOF\ execfile('add_initial_data.py') \EOF
Mazel Tov
  • 2,064
  • 14
  • 26
  • Thanks @Mazel. This project is loading initial data with running that script and using fixtures either. I can't merge the two approaches by now, so at the present I will run that script. But I didn't understand your answer. I need to run the script using `call_command` write? How to put the `< – Caco Feb 15 '18 at 18:58
  • 1
    just paste it as is... or check this answer https://stackoverflow.com/questions/16853649/executing-python-script-from-django-shell – Mazel Tov Feb 15 '18 at 19:12
  • Ok, the link in your comment solves the question using python built-in `exec` function. Specifically the `exec(open('add_initial-data.py').read())` command as I'm using python 3. Thank you. – Caco Feb 15 '18 at 20:10