Clone wiki

Migratory / DatabaseManager

DatabaseManager Reference

When you define an up() function in your migration script, it takes one argument 'database' which is a database manager. It allows you to access the previous version of your models, and then drop, change, and add your models and fields until it resembles your current These changes are also reflected in the database.


The database manager allows access of models through a __getitem__ method, so you can do:

def up(database):
     print database['app.Model'].objects.all()

Where 'app' is your app name (which you can omit if it is the app that contains your migration), and Model is the model class.


Adds the model specified by the signature (e.g. 'app.Model'), which should be defined in your current models.

rename_model(old_sig, new_sig)

Renames the model from the old signature, to the new one.


Drops the model specified by the signature from your old models.

add_field(model_sig, name)

Adds a new field to the model specified by the model signature and field-name from your new models.

change_field(model_sig, old_field_name, new_field_name = None)

Changes an old field to match the new models field, it the new field has a different name, you can specify it there.

change_field(model_sig, name)

Drops the field off the model.