[Answer]-Importing the Django's model classes – Python

1👍

Try

python manage.py shell

from graphs.models import *

and add
from .models import APITest

to parser.py and run
python manage.py shell

from graphs.parser import Parser

In order to manage dependencies in Django it is best to use python virtualenv.

If you just want to run some logic from Django from command line, try using Writing custom django-admin commands.
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

In your case if you want to have a script my_parser.py you would place it in graphs/management/commands/ folder and you can run it as python manage my_parser.

The code of my_parser would look something like this

from django.core.management.base import BaseCommand, CommandError
from .models import APITest

class Command(BaseCommand):
    args = '<arg1 arg2...>'
    help = 'Help line ...'

    def handle(self, *args, **options):
        ... # You code comes here

0👍

The first one doesn’t work because you do:

from testrun.graphs.models import *

but it should be :

from graphs.models import *

because your pythonpath is already pointing to the testrun project directory. The first import will look in the testrun/testrun/ directory, i guess that’s not what you want.

The second one doesn’t work because i assume you execute the file from the graphs directory. If you execute the file from there, the import won’t work, because the import is assuming the python path is pointing to the testrun project root directory.

Leave a comment