Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing arguments to management.call_command on a django view [duplicate]

I have a command i run from the command line that takes two arguments,-tzusb and -e. I have converted this so that it is called on a django view. I have a little glitch though, How do i pass this arguments to the management.call_command function I have in my views? here is my view for that

def tzusbcsv(request):
    management.call_command('artifact_db_loader','artefacts')
    return render_to_response('html/upload.html')
like image 784
Joshua Avatar asked Mar 16 '14 09:03

Joshua


1 Answers

In your command you should find the option definitions which should look like the following:

make_option('-tzsub', dest='tzsub', action='store_true', help='Help description...')
make_option('-e', dest='e', action='store_true', help='Help description...')

Have a look on them and take into account "dest" argument for each one. Assuming you defined dest='tzsub' for -tzsub and dest='e' for -e (like in the example above), you should call the command in this way:

management.call_command('artifact_db_loader','artefacts', tzsub=True, e=True)

This is the same of calling the command from your console like this:

python manage.py artifact_db_loader artefacts -tzsub -e

Of course if the parameters need any arguments (so you have action='store' in the option definition) simply replace the boolean argument with the value you need. For instance:

management.call_command('artifact_db_loader','artefacts', tzsub='wow!', e=7)

This is the same of calling the command in this way:

python manage.py artifact_db_loader artefacts -tzsub "wow!" -e 7
like image 136
Marco Avatar answered Sep 21 '22 11:09

Marco