Harper
Harper

Reputation: 1223

Using Django with an existing, complex database

Is it possible to use Django with an existing database, Microsoft SQL in my case, that is really huge and complex and in addition must not be changed in any way by Django?

I already read this, but it does not seem to solve my problem; it says manage.py migrate will add some tables to the database, which must not happen.

I also found this, which might not work, because it still needs to use the migration on every DB: https://docs.djangoproject.com/en/1.10/topics/db/multi-db/

My user stories are:

Beyond that, the database must not be changed.

Is that possible with Django? Is Django even the right tool for the job? Is it possible to have a Django-specific database and a different database from which it will only select/insert?

Upvotes: 2

Views: 195

Answers (2)

Mohammad Jafar Mashhadi
Mohammad Jafar Mashhadi

Reputation: 4251

You can use another database for django to keep its meta data. Database routers help you in telling Django to save which data in which database.[docs]

An example:

Settings.py

DATABASE_ROUTERS = ['path.to.YourRouter']

router.py

class YourRouter(object): 
    def db_for_read(self, model, **hints):
        if model.__class__.__name__ in ['A', 'B']:
            return 'secondDB'
        return 'default'

    def db_for_write(self, model, **hints):
        if model.__class__.__name__ in ['A', 'B']:
            return 'secondDB'
        return 'default'        
    def allow_relation(self, obj1, obj2, **hints):
        return True

To write in your visit report table, inspectdb management command can help you significantly by automatically generating models based on your existing database. [docs]

Upvotes: 3

John Gordon
John Gordon

Reputation: 33310

Django will add some tables for keeping track of various things like administrative user permissions, sessions, content types, and model migrations.

If you truly do not want ANY changes to your database, than Django is not the right tool for you.

Upvotes: 0

Related Questions