Auto loading

Back to doc index


This feature is deprecated.

It will be removed in version 4.x.

Auto loading will be enabled when D2A_CONFIG["AUTOLOAD"] is specified.


# This variable can be omitted.
    'AUTOLOAD': { # optional
        # module name: It can be used different module name from `models_sqla`.
        'module': 'models_sqla',  # optional, default: 'models_sqla'
        # transfer function's args after 'exports' arg.
        'option': {  # optional
            'db_type': 'postgresql',  # default: 'default'
            'back_type': 'backref',  # default: 'backref'

All apps

You are ready.

Then you can import sqlalchemy model on every apps.


It differs from Code generation.

Auto loaded models will save on in-memory and will not be output to a file.


>>> from books import models_sqla
>>> models_sqla.  # tab completion
models_sqla.Author(            models_sqla.BookCategory(      models_sqla.CategoryRelation(  models_sqla.transfer(
models_sqla.Book(              models_sqla.Category(          models_sqla.models

>>> models_sqla.Book
<class ''>

>>> models_sqla.Book.  # tab completion       models_sqla.Book.content      models_sqla.Book.metadata     models_sqla.Book.tags
models_sqla.Book.author_id    models_sqla.Book.description  models_sqla.Book.mro(         models_sqla.Book.title
models_sqla.Book.category           models_sqla.Book.price
# SQL Expression schema
>>> models_sqla.Book.__table__
  'book', MetaData(bind=None),
  Column('id', UUID(), table=<book>, primary_key=True, nullable=False, default=ColumnDefault(<function uuid4 at 0x7f3cebe7e598>)),
  Column('price', JSON(astext_type=Text()), table=<book>, nullable=False),
  Column('title', VARCHAR(length=255), table=<book>, nullable=False),
  Column('description', TEXT(), table=<book>),
  Column('author_id', INTEGER(), ForeignKey(''), table=<book>),
  Column('content', BYTEA(), table=<book>, nullable=False),
  Column('tags', ARRAY(VARCHAR()), table=<book>, nullable=False),

Per models module

If you want to create a module manually, create a file in an app as follows:

from d2a import transfer
from . import models
transfer(models, globals())

When exists, auto module creation will be omitted.

And if you create every manually, it is unnecessary to set d2a to settings.INSTALLED_APPS.


  • project_postgresql/books/

  • You can omit specifying db_type, then it automatically detects a database type from settings.DATABASES['default'].

    • Now postgresql, mysql and oracle are allowed, the other types will be converted to the following types as default type: sqlalchemy/

Per model

If you just want to convert one model, you should use declare function.


>>> from d2a import declare
>>> from sales.models import Sales
>>> sales = declare(Sales)
>>> sales
<class 'd2a.sales'>

>>> sales.__table__
  'sales', MetaData(bind=None),
  Column('id', BIGINT(), table=<sales>, primary_key=True, nullable=False),
  Column('book_id', UUID(), ForeignKey(''), table=<sales>, nullable=False),
  Column('sold', TIMESTAMP(), table=<sales>, nullable=False),
  Column('reservation', INTERVAL(), table=<sales>),
  Column('source', INET(), table=<sales>),

>>> sales.           sales.mro(         sales.sold
sales.book_id      sales.metadata     sales.reservation  sales.source