I have written a python program which reads data from a directory of files and now I would like to save this data into a Django database. Ideally there would be a python script within my Django project which imports the necessary models reads the data and then writes to the database but I'm not sure if this is possible? If it is what imports do I need and where in the project directory would the script be saved?
2 Answers
You can save the file in root directory (where manage.py is present) of Django project and import your models as follows:
from django.core import management
from YourProject import settings
management.setup_environ(settings)
from YourApp.models import YourModel
Or try this code:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "YourProject.settings")
from YourApp.models import YourModel
3 Comments
Gottfried
thanks for the answer but I'm getting an error - AttributeError: 'module' object has no attribute 'setup_environ'
Prashant Borde
Which Django version are you using?
Gottfried
>>> django.VERSION (1, 6, 1, 'final', 0)