I am trying to import data from csv into the django database.
models.py
class contactDetails(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
phonenumber = models.IntegerField()
email = models.CharField(max_length=100)
def __str__(self):
return self.firstname
save_form_data is project name and AppFormSaving is an app.
import.py
project_dir = 'K:\\PY\\save_form_data'
## project_dir = 'K:\\PY\\save_form_data\AppFormSaving'
sys.path.append(project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django
django.setup()
from AppFormSaving.models import contactDetails
data = csv.reader(open('K:\\sample.csv'), delimiter=",")
for row in data:
if row[0] != 'firstname':
details = contactDetails()
details.firstname = row[0]
details.lastname = row[1]
details.phonenumber = row[2]
details.email = row[3]
details.save()
when I run import.py
K:\PY\save_form_data>python import.py
I get an error below,
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 55, in __geta
ttr__
self._setup(name)
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 99, in __init
__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
ImportError: No module named 'settings'
I followed this tutorial. What am I missing here?