Access django sqlite db from external script

1.5k views Asked by At

I want to prepopulate my db using an external script. The script is the following

# -*- coding:utf-8-*-
import os, sys
from random import choice
PROJECT_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__),'..','..'))

sys.path.append(PROJECT_DIR)
os.environ['DJANGO_SETTINGS_MODULE']='geoedu.settings'

from school.models import School    
from student.models import Student

if __name__=='__main__':
    student = Student(first_name=choice(first_names_males), last_name=choice(last_names_males),
                  father_name=choice(first_names_males), mother_name=choice(first_names_females),
                  mobile=choice(mobiles), telephone=choice(telephones))
    student.save()

where the arguments in choice are lists with names and telephones. When i run it though i get the following error

django.db.utils.DatabaseError: no such table: student_student

The directory tree

geoedu
├── geoedu
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── geoedu.db
├── geoedu.sublime-project
├── geoedu.sublime-workspace
├── manage.py
├── school
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   └── views.py
├── scripts
│   └── populate.py
└── student
    ├── admin.py
    ├── admin.pyc
    ├── __init__.py
    ├── __init__.pyc
    ├── models.py
    ├── models.pyc
    ├── tests.py
    └── views.py

What it does is it creates a new geoedu.db inside the script folder and uses that(which is empty and has no student_student table ofcourse)

Why is that happening?The PROJECT_DIR is the correct one(printed it out) and is the root of the tree that is the geodedu on the top of all.

EDIT: new geoedu.db gets created when creating new student. If I comment out those lines geoedu.db doesn't get created.

2

There are 2 answers

1
Raphaël Braud On

Defining models in Django helps you to manipulate instances.

But, before, you have to create the table "skeleton" in your sqlite db

For this, you just need to synchronize the db with Django by doing :

python manage.py syncdb

This has to be done one time only.

In the future, you will probably needs to make your models evolve (thus needing similar db evolutions), at this time South - or adhoc schema evolution queries - will be useful (http://south.aeracode.org/) but as far as you're prototyping, you can just remove db and recreate it with good table by executing syncdb

0
Apostolos On

The problem was that i hadn't used an absolute path for the database name in the settings.py file. I added this in the NAME of the database engine

'NAME':os.path.join(SETTINGS_DIR,'geoedu.db')

and everything worked as it should. PROJECT_DIR is

SETTINGS_DIR = os.path.dirname(__file__)

so the database is created inside settings folder. If you wanted inside projects folder you should do

'NAME':os.path.abspath(os.path.join(SETTINGS_DIR,'..','geoedu.db'))