In my Django project I have an app called 'catalog', here's the model in models.py
:
from django.db import models
class Catalog(models.Model):
brand = models.CharField(max_length=255)
supplier = models.CharField(max_length=255)
catalog_code = models.CharField(max_length=255, null=True, blank=True)
collection = models.CharField(max_length=255)
season = models.CharField(max_length=255)
size_group_code = models.CharField(max_length=2)
currency = models.CharField(max_length=3)
target_area = models.CharField(max_length=255)
I want to create one Catalog object but I don't want to do it using the admin panel or a form, so in the same app 'catalog' I've created tools.py
which is just a script, it reads a csv file and create an istance of Catalog()
.
from catalog.models import Catalog
def create_catalog(file):
with open(file, 'r') as f:
f = f.readlines()
catalog = Catalog(# all my keyword args)
catalog.save()
I don't know if the script is fine but it's not important, my problem is when I try to run tools.py
froom my IDE I get this error:
raise ImproperlyConfigured(
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 tried to run it from the shell with:
exec(open('catalog/tools.py').read())
but nothing happens and no traceback.
So how can I run a Python script inside of a Django project that creates objs in my DB? I'm already aware of: How to execute a Python script from the Django shell? and this. but no solution worked.