I'm currently learning to use Django with a Udemy course that is pretty outdated but since it was the best rated I thought I'd give it a try. After covered how to set the models.py file they showed how to use Faker to populate the admin section with fake data. The problem arises when trying to run this populate file, this is what shows up:
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.
This is my populate file
from faker import Faker
from first_app.models import AccessRecord, Webpage, Topic
import random
import django
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
# FAKE POP SCRIPT
fakegen = Faker()
topics = ['Search', 'Social', 'Market', 'News', 'Games']
def add_topic():
t = Topic.objects.get_or_create(top_name=random.choice(topics))[0]
t.save()
return t
def populate(N=5):
for entry in range(N):
# get the topic for the entry
top = add_topic()
# create the fake data for that entry
fake_url = fakegen.url()
fake_date = fakegen.date()
fake_name = fakegen.company()
# create the new webpage entry
webpg = Webpage.objects.get_or_create(
topic=top, url=fake_url, name=fake_name)[0]
# create a fake access record for that webpage
acc_rec = AccessRecord.objects.get_or_create(
name=webpg, date=fake_date)[0]
if __name__ == '__main__':
print('populating script..')
populate(20)
print('Populating complete')