Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
Django:
Legacy DB
Integrations
How it is possible to integrate a django project
with a legacy database system
• Django is best suited for developing new applications.
• In case you want to use it in an already existing ecosystem you...
• Standard way:
• Let django creates users, group and permission tables in a database for you.
• All the built-ins work we...
• Configure the database parameters into the settings.py
• Do one or more of the followings:
• Run inspectdb over your sch...
• Django tries to write all models for you by reverse engineering your database.
• By default all the generated models are...
• 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 create your models starting from a logical or materialized view in
the same way you do for the tables.
• You hav...
• The model need to have a custom Manager (the one who retrieves the data and
instantiates the model)
• The custom manager...
• Django provide a way to perform query directly to the db via django.db.connection:
https://docs.djangoproject.com/en/1.7...
Constraints Model
instances
ORM query
and filtering
on the same
model
ORM
relationship
queries
ORM Insert,
Update and
Dele...
Próxima SlideShare
Cargando en…5
×

Django legacy database integrations

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.

  • Inicia sesión para ver los comentarios

Django legacy database integrations

  1. 1. Django: Legacy DB Integrations How it is possible to integrate a django project with a legacy database system
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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

×