2

I'm trying to create a telegram bot which uploads images to a django website. You send to the bot a picture -> bot uploads the picture to the server and adds info to the model -> picture appears on the webpage

I'm stuck with the "bot adds the info to the model" part. Basically, what I want to do is to use the django.core.management.call_command to call a custom command from code which adds info.

Here is the structure

website/
    bot.py
    manage.py
    myapp/
        __init__.py
        management/
            __init__.py
            commands/
                __init__.py
                myapp_task.py
        views.py

Here is my custom command

# myapp_task.py
from django.core.management.base import BaseCommand
from picfeed.models import Captions

class Command(BaseCommand):
    help = 'Adds captions to the model'

    def add_arguments(self, parser):
        parser.add_argument('cap_text', type=str)

    def handle(self, *args, **options):
        cap_text = options['cap_text']
        caption = Captions(cap_text = cap_text)
        caption.save()
        self.stdout.write(self.style.SUCCESS('Successfully added caption "%s"' % cap_text))

When I use Terminal and python manage.py myapp_task 'some string to add to the model' works just perfectly fine.

However, when I call myapp_task from the code it doesn't work:

#bot.py
from django.core.management import call_command

call_command('myapp_task',cap_text = 'Hello there!' )

The error:

 \website>python bot.py
Traceback (most recent call last):
  File "C:\Users\EgOl7001\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\management\__init__.py", line 103, in call_command
    app_name = get_commands()[command_name]
KeyError: 'myapp_task'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "bot.py", line 3, in <module>
    call_command('myapp_task',cap_text = 'Hello there!' )
  File "C:\Users\EgOl7001\AppData\Local\Continuum\anaconda3\lib\site-packages\django\core\management\__init__.py", line 105, in call_command
    raise CommandError("Unknown command: %r" % command_name)
django.core.management.base.CommandError: Unknown command: 'myapp_task'

I would appreciate your help! :)

1 Answer 1

1

You're trying to use all django toolbox without setting your configuration.

You should try to add, in your bot.py something like that before using django:

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings")
django.setup()

https://docs.djangoproject.com/en/3.0/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.