(y) xlm@d8cb8a102c73:~/workspace/y-server/y$ ./manage.py
run_algorithm --settings=y.settings.local
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/xlm/.virtualenvs/y/lib/python3.5/site-
packages/django/core/management/__init__.py", line 354, in
execute_from_command_line
utility.execute()
File "/home/xlm/.virtualenvs/y/lib/python3.5/site-
packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/xlm/.virtualenvs/y/lib/python3.5/site-
packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/xlm/.virtualenvs/y/lib/python3.5/site-
packages/django/core/management/base.py", line 454, in execute
self.stdout.write(output)
File "/home/xlm/.virtualenvs/y/lib/python3.5/site-
packages/django/core/management/base.py", line 111, in write
if ending and not msg.endswith(ending):
AttributeError: 'dict' object has no attribute 'endswith'
I am trying to run the file algorithm.py with the command file run_algorithm.py (below) on a django framework. However I get the above attribute error, and I am unsure why.
import logging
from django.conf import settings
from django.core.management.base import BaseCommand
from ...algorithm import engagement_level_mapping
logger = logging.getLogger(settings.LOGGER_NAME)
class Command(BaseCommand):
help = "TODO: describe"
def handle(self, *args, **options):
result = engagement_level_mapping()
return result
However, when printing the result, rather than returning, the file executes correctly.
import logging
from django.conf import settings
from django.core.management.base import BaseCommand
from ...algorithm import engagement_level_mapping
logger = logging.getLogger(settings.LOGGER_NAME)
class Command(BaseCommand):
help = "TODO: describe"
def handle(self, *args, **options):
result = engagement_level_mapping()
print(result)
The command file prints a dictionary of engagement values for each user (not that relevant, but just that it prints a dictionary).
Here is the code in base.py
You get this error because a django management command's handle() method is not supposed to return anything but a string -if you read the traceback, you easily find out that the error happens in the base class (in django/core/management/base.pydjango/core/management/base.py), which tries to print out the value returned by your command.
The point is that is makes no sense returning anything else from a management command: a management command is a command-line tool and is expected either to work by side effects (modifying something in your database / on your filesystem / etc) or to generate some text output that can be either displayed or fed to another command line program (in the unix tradition where you pipe commands together).
The fix is plain obvious: format your result as a string before returning it, ie (example with a json output but you could choose to generate csv or xml or whatever suits you):
import json
class Command(BaseCommand):
help = "TODO: describe"
def handle(self, *args, **options):
result = engagement_level_mapping()
return json.dumps(result, indent=2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With