Skip to main content

It converts django models to a sqlalchemy orm/expression objects.

Project description


  • Python: 2.7, 3.3, 3.4, 3.5, 3.6 (Tested with 2.7, 3.6)
  • Django: 1.9 ~ 2.0 (Tested with 1.11, 2.0)
  • SQLAlchemy: 0.9 ~ 1.2 (Tested with 1.2)


$ pip install d2a



Example: you make at the same directory which has been placed on.

That’s all, you can import sqlalchemy declaration made from django model.

>>> 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),

Also, it can extract model declared implicitly depending on m2m field. (in this case, BookCategory)


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

Custom fields

If you are using customized field which is not built-in, you can register the field as the other field using alias method.

from django.db.models import ImageField

class ExtendedImageField(ImageField):
    """something customizing"""

from d2a import alias
alias(ExtendedImageField, ImageField)


start up environment

$ git clone
$ cd d2a
$ docker-compose up


$ docker exec -it d2a_app_1 /bin/bash
# python -m venv venv # only first time
# source venv/bin/activate
(venv) # cd project_postgresql/ # (or mysql)
(venv) project_postgresql # ./ migrate


(venv) project_postgresql # ./ shell
>>> from books import models_sqla
>>> book = models_sqla.Book()
>>> author = models_sqla.Author()
>>> = author
>>> author.books
[< object at 0x7f3cec539358>]
# And do something you want do ;)


  • (2018-07-10)
  • Improved a little.
  • (2018-07-06)
  • Fixed a bug, that it will be provided None even though it does not be specified default argument.
  • (2018-07-05)
  • Fixed bugs.
  • Added unit tests.
  • Fixed a bug that abstract models become the targets.
  • Deleted install_requires.
  • added alias method.
  • fixed bugs.
  • it got easy to declare custom field.
  • transfer method can define secondary table.
  • it supported m2m field.
  • it limited django version less than 1.9.

first release (2017-12-27)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
d2a-1.0.2.tar.gz (9.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page