Django - Call command line with arguments from view

时间:2018-06-04 16:48:46

标签: python django django-management-command

I am trying to call a command that alters a database from a view. Basically, once the view is loaded, a parameter changes. If I run the command from the terminal, it works, but if I try to call it from the view I keep on getting this error.
I would believe there is an error in the way I pass the dictionary as the command works fine from the terminal. It may also be because I am trying to update a model which is being shown to the user at the moment, but I have also tried passing a different ID as an argument and it raises the same error.

  CommandError at /sample/45/

Error: argument num: invalid int value: "{'num': 45}"

Request Method:     GET
Request URL:    http://127.0.0.1:8000/sample/45/
Django Version:     2.0
Exception Type:     CommandError
Exception Value:    

Error: argument num: invalid int value: "{'num': 45}"

Exception Location:     /usr/local/lib/python3.6/dist-packages/django/core/management/base.py in error, line 60
Python Executable:  /usr/bin/python3
Python Version:     3.6.5
Python Path:    

['/home/ander/Desktop/Proyecto/meduloblastoma/Code/MedulloblastomaProject',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/usr/local/lib/python3.6/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/home/ander/Desktop/Proyecto/meduloblastoma/Code/MedulloblastomaProject']

Server time:    Mon, 4 Jun 2018 18:35:37 +0200

This is the code I have used.

classify.py

from django.core.management.base import BaseCommand
from rpy2.robjects.packages import importr
import rpy2.robjects as ro
from sample.models import Sample

class Command(BaseCommand):
    """
    Management command to run the classifier
    """
    def add_arguments(self, parser):
        parser.add_argument('num',type=int)

help="Runs the classifier on uploaded samples"

def handle(self,*Args,**Options):
    num=Options['num']
    samples=Sample.objects.filter(id=num)
    print(samples)
    for sample in samples:
        if sample.classificator=="Not classified":
            sample.classificator="Classified"

views.py

@login_required
def detail_sample(request, id):
    instance = get_object_or_404(Sample, id=id)
    call_command('classify', {'num': instance.id})
    if instance.user == request.user:

        if instance.allow:
            agreed="This data is allowed to be used for improvement of the algorithm"
        else:
            agreed="This data is NOT allowed to be used for improvement of the algorithm"

    context={
        "title": instance.title,
        "user":instance.user.username,
        "comment": instance.comment,
        "time": instance.time,
        "diagnosis": instance.diagnosis,
        "gender": instance.gender,
        "file1": instance.file1,
        "file2":instance.file2,
        "id": instance.id,
        "a":instance.classificator, #a[0][2:],
        "b":instance.classificator, #a[1][0],
        "agree":agreed,
        "allow":instance.save,
    }
    return render(request, "sample/sample_detail.html", context)
else:
    raise Http404

1 个答案:

答案 0 :(得分:0)

You should pass num as a keyword argument:

call_command('classify', num=instance.id)

However, might be better to factor out your code into a separate method.

def classify(num):
    samples=Sample.objects.filter(id=num)

    for sample in samples:
        if sample.classificator=="Not classified":
            sample.classificator="Classified"

Then your management command and view can both call classify(num), and you don't have to use call_command.