How it is possible to integrate a django project
with a legacy database system.
This slides show almost all strategies that can be used with a table summary at the end.
2. ⢠Django is best suited for developing new applications.
⢠In case you want to use it in an already existing ecosystem you have to bear
in mind:
⢠To use all the built-in applications you need to create some tables.
⢠You can use many databases, one for the legacy data and one for the
djangoâs functionalities
⢠If your database is not well designed probably youâll lost some of the
djangoâs orm features.
⢠Better if your legacy database is read-only
Assumptions
3. ⢠Standard way:
⢠Let django creates users, group and permission tables in a database for you.
⢠All the built-ins work well.
⢠Custom way:
⢠Use your authentication system writing a custom authenticationâs backend and plug
it in django
⢠Write a custom user model based on your legacy table and substitute the django
user model with it.
⢠You have to implements some methods in order to maintain compatibility with the
djangoâs built-ins
⢠Example: http://tomforb.es/using-a-custom-sqlalchemy-users-model-with-django
Django
authentication system
4. ⢠Configure the database parameters into the settings.py
⢠Do one or more of the followings:
⢠Run inspectdb over your schema
⢠Manually create models from tables
⢠Manually create unmanaged models from database views
⢠Manually create unmanaged models from RawQuerySet
⢠Use Python DB API or another ORM
Integrate django with your legacy db
5. ⢠Django tries to write all models for you by reverse engineering your database.
⢠By default all the generated models are unmanaged (you canât apply
migrations)
⢠Inspectdb is meant as a shortcut, not as definitive model generation. Indeed
youâll have to look over the generated models yourself to make customizations:
⢠Make sure that all models have a primary key
⢠Rearrange modelsâ order
⢠Rename modelsâ fields with more meaningful names
Inspectdb command
python manage.py inspectdb > models.py
6. ⢠Naming:
⢠You can call your model and its fields as you like.
⢠You can specify the table name inside modelâs Meta class
⢠You can specifythe columnâ s names using the fieldâattribute column_name
⢠Foreign Keys:
⢠You can specify all the foreign keys you tables has.
⢠You can also specify foreign keys if there arenât real constraint into the table
⢠It is sufficient that the column used as foreign key contains the related tableâs ids.
⢠You donât have to specify all columns as fields, just the one you will use
⢠You have to specify the primary key field
Manually create models from tables
7. ⢠You can create your models starting from a logical or materialized view in
the same way you do for the tables.
⢠You have to specify the primary key field
⢠Same Naming and Foreign keys rules
⢠You cannot use these models to insert, delete or update
⢠They must be unmanaged
⢠You need databases authorizations in order to create custom views
Manually create models from views
8. ⢠The model need to have a custom Manager (the one who retrieves the data and
instantiates the model)
⢠The custom manager use a RawQuerySet object to query the db.
⢠The query must retrieve all the columns specified in the model
⢠The query can span across multiple tables
⢠You have to specify the primary key field
⢠Same Naming and Foreign keys rules
⢠You canât use Django ORM features, you have to implements all the query methods you
need
⢠RawQuerySet allows you to make parameterized queries
Manually create models from
RawQuerySet
9. ⢠Django provide a way to perform query directly to the db via django.db.connection:
https://docs.djangoproject.com/en/1.7/topics/db/sql/#executing-custom-sql-directly
⢠You can execute either SELECT, INSERT, DELETE or UPDATE but you canât use djangoâs
models.
⢠Alternatively you can use a completely different ORM like SQLAlchemy to manage your
database.
⢠Both these methods cannot be compatible with many of the built-in features of django.
Use Python DB API or other ORM
10. Constraints Model
instances
ORM query
and filtering
on the same
model
ORM
relationship
queries
ORM Insert,
Update and
Delete
Operations
Django Admin Django
Migrate
Inspectdb ⢠Every model must have only
one field with PrimaryKey =
True
⢠Inspectdb runs across all
tables in the schema
⢠Manually check the models
compliance
YES YES YES If the
related models
are ORM query
compliance
YES YES YES if
managed =
True
Manually
created
models from
tables
⢠Every model must have only
one field with PrimaryKey =
True
⢠You have to create a model
for each needed table
YES YES YES If the
related models
are ORM query
compliance
YES YES YES if
managed =
True
Manually
created
unmanaged
models from
views
⢠Every model must have only
one field with PrimaryKey =
True (easly done inside sql
with row_count)
⢠Database write permission
needed
YES YES YES If the
related models
are ORM query
compliance
NO YES but read
only
NO
RawQuerySet ⢠Every model must have only
one field with PrimaryKey =
True (easly done inside sql
with row_count)
⢠You have to write raw sql
inside pythonâs scripts
YES NO, you
have to
write every
query
method by
yourself
YES If the
related models
are ORM query
compliance
NO NO NO
Python DB
API or other
ORMs
⢠You have to do all by hand!!! NO NO NO NO NO NO
Database integrations summary