0

I cloned a GitHub repo. Everything is working fine. But I need to populate sample data for all the endpoints. There about 20 files with sample data for each endpoint. Then there is a file(dataload.py) in the root folder that should call all those 20 files and populate the database.

I ran python dataload.py but I got the error

File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

Dataload image

This is the content of dataload.py

from subprocess import call

print ('**** starting ***')

SETTINGS_FILE= 'promedic.settings_prod'
# SETTINGS_FILE= 'promedic.settings'

call(['python', 'manage.py', 'makemigrations', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'migrate', '--settings=%s'% SETTINGS_FILE])


call(['python', 'manage.py', 'loaddata', 'core/fixtures/allergies.json', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'loaddata', 'core/fixtures/blood_group.json', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'loaddata', 'core/fixtures/disabilities.json', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'loaddata', 'core/fixtures/drug-forms.json', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'loaddata', 'core/fixtures/drug-brands.json', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'loaddata', 'core/fixtures/dispense-types.json', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'loaddata', 'core/fixtures/genotypes.json', '--settings=%s'% SETTINGS_FILE])
call(['python', 'manage.py', 'loaddata', 'core/fixtures/states.json', '--settings=%s'% SETTINGS_FILE])

I have change dataload.py to

from django.core.management import call_command
#from subprocess import call

print ('**** starting ***')

SETTINGS_FILE= 'promedic.settings_prod'
# SETTINGS_FILE= 'promedic.settings'
'''
call_command('makemigrations', )
call_command('migrate')
'''
call_command('loaddata', 'core/fixtures/allergies.json', settings='SETTINGS_FILE')
call_command('loaddata', 'core/fixtures/blood_group.json', settings='SETTINGS_FILE')

Now getting this error

in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
techstack
  • 1,367
  • 4
  • 30
  • 61

1 Answers1

0

There is a third-party library django-extensionslink can do this work for you. create a scripts folder from your one of your app and run your desire python script from that, or as this seems like for fixture data loading, you can load these fixtures from your script.

Say app structure is like this

|-main_app
      | - models.py ... ( etc )
      | - fixture_folder
           |-fixtures
      | - scripts_folder
           | - __init__.py
           | load_data.py
| - manage.py

Your load_data.py may like this

from django.core.management import call_command


def run():
    call_command('loaddata', 'fixture_file.json')

call_command can actually do a lots of thinglink

Shakil
  • 4,520
  • 3
  • 26
  • 36