Table Of Contents

Add some Bootstrap Data

Using the shell command

After creating your tables, you can test them and add rows using an interactive Python shell suited to your project. To start the shell, type:

$ tg-admin shell

Any data you add or modify in this shell is handled in a transaction. If you quit without committing, no changes will be written to the database. To commit the transaction, type this in the shell after making your changes:

>>> session.flush()

Note

SQLObject user use:

>>> hub.commit()

Here’s how I tested my tables using the interactive shell. You should follow along in order to insert some items into your database:

>>> u1 = User(email="exogen@gmail.com")
>>> u1.email
'exogen@gmail.com'

>>> u1.id
1

>>> u1.lists
[]

>>> l1 = List(title="Groceries", user=u1)
>>> l1.title
u'Groceries'

>>> l1.user
<User 1 email='exogen@gmail.com'>

>>> l1.user.email
'exogen@gmail.com'

>>> l1.items
[]

>>> u1.lists
[<List 1 title=u'Groceries' userID=1>]

>>> i1 = Item(list=l1, value="Milk")
>>> i2 = Item(list=l1, value="Eggs")
>>> i3 = Item(list=l1, value="Bread")
>>> [item.value for item in l1.items]
[u'Milk', u'Eggs', u'Bread']

>>> len(l1.items)
3

Remember to commit your changes if you want them to be saved:

>>> session.flush()

If your tables are working and sufficient, then you can close model.py, you won’t need to change it for the rest this tutorial.

Using CatWalk

Run:

$ tg-admin toolbox

and select Catwalk. Note that CatWalk works only with SQLObject, however.

Using the bootstrap command

When you install a TurboGears application with the name myapp using python setup.py install or python setup.py develop, then this will also install a start script with the name start-myapp and a “bootstrap” script bootstrap-myapp. The latter will call the bootstrap_model() function inside your model.py file. By default this calls two functions to create all database tables for your model and optionally create a user (if you run the script with the --user option. This is a very convenient way to create a starter database, as you can easily extend this mechanism by calling more functions filling other tables with data to start with.