The following is a snippit of code that I use to pre-populate a database with sample values: (note that it isn't simply redirecting a file to tg-admin shell.)

# encoding: utf-8
package_name = 'bobsyouruncle'

import pkg_resources
pkg_resources.require('TurboGears')

import sys
from os.path import *
import turbogears
from datetime import datetime

print 'Loading configuration...'

turbogears.update_config(configfile=package_name+'/config/app.cfg', modulename=package_name+'.config')

if len(sys.argv) > 1:
turbogears.update_config(configfile=sys.argv[1], modulename=package_name + '.config')
elif exists(join(dirname(__file__), 'setup.py')):
turbogears.update_config(configfile='dev.cfg', modulename=package_name + '.config')
else:
turbogears.update_config(configfile='prod.cfg', modulename=package_name + '.config')

print 'Finished loading configuration.'
import bobsyouruncle.model as model

# I use a split model.
from bobsyouruncle.model.base import *
from bobsyouruncle.model.support import *

model.hub.begin()

try:
# Perform actions here to create data.  E.g.:
Province.init_data()
Language.init_data()

except:
model.hub.rollback()
print 'Error: Could not initialize database!  No data committed.  Traceback follows.'
raise

model.hub.commit()
print 'Successfully added stock data.'

Individual classes which will -always- have stock data (like Province, Country, Language, etc.) have a classmethod called init_data which returns a list of created records, usually with the following structure:

@classmethod
def init_data(self):
data = "">
dict(iso='en', name="English"),
dict(iso='fr', name="Français")
]
return [self(**i) for i in data]

I strongly recommend adding this functionality to tg-admin sql create - one pass to create the tables, one pass to run any init_data or _init_data or _init method.  That would make my life a bit simpler.  ^_^

Matthew Bevan, Systems Administrator
Top Floor Computer Systems Ltd.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to