Use the existing database with django python


I have existing database which is being used for with another application with python and sqlalchemy. Now i want to build the REST API end point as i have to build the wrapper around current code.

I want to use Django and i have few doubts

  1. Can i use same database for Django related tables so that my other tables are also there and some django generated tables are aslo in same database

  2. I want to slowly shift my app from sqlachemy to django ORM but to start with i have to used both . I mean some models in Django ORM and rest in SqlAlchemy . Is this possible

It is possible to use an existing database with django.

You will have to tell django to ignore migrations in this database tables, and to use the actual existing table names, instead of the django default applabel_model naming:

class ModelForExistingTable(models.Model):

    class Meta:
       managed = False
       db_table = "ExistingTableName"

To your questions:

  1. Django doesn't care of other tables, as long as the required tables for the django models exists and match the constrains, indexes etc which you define in django.

  2. You can shift from one ORM to another. If you use both, you will have two objects mapping and calls in the same app. It want be easy to handle both, to pass data between objects, consolidate data for views, etc.

Consider to move everything to django altogether. Django ORM is mature, feature rich, and unless you have some edge case - can probably handle everything that Sqlalchemy does.

Moving to django ORM is mainly to define the models properly. From there on it's much easier since django inherits the model logic to querysets, forms, and so does the REST frameworks. Once the models are well defined, the logic is typically available to other components of the framework.