2

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:

  • I want to view the customer data from the existing database in my browser (SQL select)
  • I want to write a visit report (SQL insert)

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?

2 Answers 2

3

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]

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

Comments

0

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.

2 Comments

Ok, thanks. The DB must not be changed, because it is a part of a software that is maintained by a different company / software and I just want to view the data in the browser and make a specific insert.
You can use Django with more than one DB. You can have Django set it's stuff up in one and connect to the other and limit it's write permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.