So at the moment, I've written the code to make calls to an API and write it to a file on my machine so I can go through it and figure out which values are located where, etc. I have also written the code to open the file and loop through the values. All I need to do now (as far as I'm aware) is write the piece of code that instantiates models using values provided by the data pulled from the API. The problem is, I have no idea where to put this code and how to call it.
The code I've written is all in a separate python file located in the same directory as my Django app.I thought I could run the code from that file if I imported Django's models and the model I'm trying to create, so I added
from django.db import models
from models import MyModel
to the beginning but was given the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I appreciate that the traceback is telling me exactly what to do, but I have no idea what that actually means I'm supposed to do.
Here's the gist of my code for reading the file with the data stored in it and creating models with it:
with open('all-moves-formatted.json', 'r') as nf:
data = json.load(nf)
for move in data:
MyModel.objects.create(
value1 = move['value1'],
value2 = move['some_dict']['value2'],
...
)
Bear in mind the file is a list of json objects, so that's why the syntax might seem a little strange.