4

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

2 Answers 2

3

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.

see https://docs.djangoproject.com/en/1.9/howto/legacy-databases/.

Sign up to request clarification or add additional context in comments.

2 Comments

so it means , to start with i can put in database settings same database as for sqlalchmey and then suppose if i have only one model in models.py and then if i use python manage.py migrate then django will auto create its tables without touching other tables. Am i right?
Great solution @Aviah it's in django1.9+ only ?
0

Yes - you can use the same database with two ORMs. You just have to create the Django models in Django and tell it what table it is for (you do this by creating the Meta subclass on your model and supply the db_table property). You can manage your other tables with SQLAlchemy as you do right now. Django doesn't impose it's own ORM, it's your choice whether to use it or not.

Here's a piece of documentation to get you started https://docs.djangoproject.com/en/1.9/howto/legacy-databases/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.