erik.dreyer
erik.dreyer

Reputation: 17

How do I run a script that uses django in the background

This is my code, I want to write data to my django database with a python script, because the data is generated and saved in a json file about once a day. The new data needs to be written to my django databse when the new json file is created

def write_stuff(self):
    """Write stuff to database"""
    var = Match(match_id=self.match_id, match_date=self.match_date, team_a=self.team_a, team_b=self.team_b,
                    winner=self.winner, event=self.event, closed=self.closed)
    var.save()

When I run this code I keep on getting an error:

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I have tried everything on SO to set DJANGO_SETTINGS_MODULE and even tried every possible path in the manage.py and wsgi.py files. What am I doing wrong?

Upvotes: 0

Views: 450

Answers (1)

James Hardy
James Hardy

Reputation: 520

I suspect it is not bootstrapping Django

I would look into making a custom Django management command.

which you can then run with

./manage.py importcsv

This will run a command within the Django environment with all settings initialised correctly

Upvotes: 1

Related Questions