Problem using syncdb() from another program

7 views
Skip to first unread message

GinTon

unread,
Nov 22, 2006, 11:07:35 AM11/22/06
to Django developers
This code activates a new application adding it in the settings file,
and then it runs the syncdb() from Django. But the problem is that if
is run inner of 'if not is_app_activate:' it makes nothing

It looks like more of a django problem than a python problem. I cannot
to know what is up here without knowing what syncdb and the rest of
that code do, which would involve me learning django code. Any
developer could help?

----------------------------------------
def main():
settings_file = './settings.py'
i18n_model = setup_environ(settings_file)

def setup_environ(settings):
"""Configure the runtime environment.
"""
if not os.path.exists(settings):
print "Error! you are not into a Django's project directory"
sys.exit(1)

project_directory = os.path.abspath('')
project_name = os.path.basename(project_directory)
application_name = "%s.I18n" % project_name

# Set DJANGO_SETTINGS_MODULE appropriately.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings" % project_name

# Add parent's directory to sys.path so that the module is
importable.
sys.path.append(os.path.dirname(project_directory))
i18n_model = __import__("%s.models" % (application_name), {}, {},
[''])
sys.path.pop()

# Looking for if the I18n application is installed.
is_header = is_app_activate = False
readfile_line = 0
for line in open(settings):
readfile_line += 1
if 'INSTALLED_APPS' in line:
is_header = True
if is_header:
if application_name in line:
is_app_activate = True
break
elif ')' in line: # To know where inserting the line.
break

# If it is not installed, it looking for the line and insert it.
if not is_app_activate:
print "Activating I18n application ..."
writefile_line = 0
# for line in fileinput.input(settings, inplace=1):
a = fileinput.input(settings, inplace=1)
for line in a:
writefile_line += 1
if writefile_line == readfile_line:
print " '%s'," % application_name
print line[:-1]
else:
print line[:-1]
a.close()
update() # * read note down

return i18n_model

def update():
# Update the data base.
try:
from django.core.management import syncdb
except ImportError, err:
print "Can't import from Django: %s" % err
sys.exit(1)

syncdb()
----------------------------------------

1- To checking if is a problem of file flushed, I added this to the
line where is the note:
-------------------------
# update()
is_app_activate = True

if is_app_activate:
update()
-------------------------

2- To check that the application is not installed:
$ tail settings.py
# Always use forward slashes, even on Windows.
)

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
)

3- Clean the data base:
$ rm data.db; python manage.py syncdb

4- Run the program: (as you can see the application has been added but
syncdb() doesn't run)
$ python load_data.py
Activating I18n application ...

5- Run again: (and now it's run)
$ python I18n/load_data.py
Creating table I18n_language
Creating table I18n_area
Creating table I18n_country
Creating table I18n_countrylanguage
Creating table I18n_phone
Creating table I18n_timezone
Installing index for I18n.Area model
Installing index for I18n.CountryLanguage model
Installing index for I18n.Phone model
Installing index for I18n.TimeZone model


So that I beleive that the file is not being flushed.

GinTon

unread,
Nov 22, 2006, 1:45:45 PM11/22/06
to Django developers
I've added pdb.set_trace() to debug.

- When I run the program in 4 step, I get the next exception when the
program is exited.

Exception exceptions.AttributeError: "'NoneType' object has no
attribute 'path'" in <bound method FileInput.__del__ of
<fileinput.FileInput instance at 0xb7d2234c>> ignored

GinTon

unread,
Nov 22, 2006, 3:12:01 PM11/22/06
to Django developers
$ python I18n/load_data.py
Activating I18n application ...

* Checking if new application has been added in settings.py *

# Always use forward slashes, even on Windows.
)

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',

'yuiyu.I18n',
)
> /usr/lib/python2.4/site-packages/django/core/management.py(451)syncdb()
-> for app_name in settings.INSTALLED_APPS:
(Pdb) p settings.INSTALLED_APPS
['django.contrib.admin', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.sessions',
'django.contrib.sites']

I think that there is a Django problem in 'management.py' program.
syncdb() is not getting all installed applications

GinTon

unread,
Nov 22, 2006, 4:40:43 PM11/22/06
to Django developers
The problem is that the method is imported before of updated the data,
so it doesn't get the last application added.

The solution is trying that syncdb() will be imported just after of
adding the new application.

Reply all
Reply to author
Forward
0 new messages