Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a python script in Django?

I am new to Django, and I'm trying to import one of my models in a script as we do it in views.py. I'm getting an error:

Traceback (most recent call last):

  File "CallCenter\make_call.py", line 3, in <module>

    from .models import Campaign


ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package

My file structure is like:

MyApp\CallCenter\

CallCenter contains __init__.py, make_call.py, models.py, views.py and MyApp has manage.py

from twilio.rest import Client
from twilio.twiml.voice_response import VoiceResponse, Say, Dial, Number, VoiceResponse
from .models import Campaign


def create_xml():

    # Creates XML
    response = VoiceResponse()
    campaign = Campaign.objects.get(pk=1)
    response.say(campaign.campaign_text)

    return response


xml = create_xml()
print(xml)
like image 732
Ayodele Ashad Avatar asked Dec 05 '25 12:12

Ayodele Ashad


1 Answers

In general, it's better to refactor "ad-hoc" scripts – anything you might run manually from a command line, say – into management commands.

That way the Django runtime is set up correctly once things get to your code, and you get command-line parsing for free too.

Your make_call.py might become something like this:

CallCenter/management/commands/make_call.py

from twilio.rest import Client
from twilio.twiml.voice_response import VoiceResponse, Say, Dial, Number, VoiceResponse
from CallCenter.models import Campaign

from django.core.management import BaseCommand


def create_xml(campaign):
    # Creates XML
    response = VoiceResponse()
    response.say(campaign.campaign_text)
    return response


class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument("--campaign-id", required=True, type=int)

    def handle(self, campaign_id, **options):
        campaign = Campaign.objects.get(pk=campaign_id)
        xml = create_xml(campaign)
        print(xml)

and it would be invoked with

$ python manage.py make_call --campaign-id=1

from wherever your manage.py is.

(Remember to have an __init__.py file in both the management/ and the management/commands/ folders.)

like image 125
AKX Avatar answered Dec 07 '25 01:12

AKX