db.backends.base.schema.BaseDatabaseSchemaEditor

class BaseDatabaseSchemaEditor [source]

Django’s migration system is split into two parts; the logic for calculating and storing what operations should be run (django.db.migrations), and the database abstraction layer that turns things like “create a model” or “delete a field” into SQL - which is the job of the SchemaEditor.

It’s unlikely that you will want to interact directly with SchemaEditor as a normal developer using Django, but if you want to write your own migration system, or have more advanced needs, it’s a lot nicer than writing SQL.

Each database backend in Django supplies its own version of SchemaEditor, and it’s always accessible via the connection.schema_editor() context manager:

with connection.schema_editor() as schema_editor:
    schema_editor.delete_model(MyModel)

It must be used via the context manager as this allows it to manage things like transactions and deferred SQL (like creating ForeignKey constraints).

It exposes all possible operations as methods, that should be called in the order you wish changes to be applied. Some possible operations or types of change are not possible on all databases - for example, MyISAM does not support foreign key constraints.

If you are writing or maintaining a third-party database backend for Django, you will need to provide a SchemaEditor implementation in order to work with 1.7’s migration functionality - however, as long as your database is relatively standard in its use of SQL and relational design, you should be able to subclass one of the built-in Django SchemaEditor classes and just tweak the syntax a little. Also note that there are a few new database features that migrations will look for: can_rollback_ddl and supports_combined_alters are the most important.

doc_Django
2016-10-09 18:35:06
Comments
Leave a Comment

Please login to continue.