2

I am having a database with some data filled in it and i want to use it in my new django app. So is their any way to use data of database in my django app.Actually i don't want to make any changes in my old database and only want to use its data. Anybody please suggest me what will be the better approach to do this.

While serching i also found a command-inspectdb which can generate model.py file from database, but their are some issues with it that it does'nt map the foreign key in model.py, we need to rearrange our classes in model.py file and some more. So i am searching for some other alternative.

8
  • 3
    there's not much point using Django if you don't create models to match your db data. you should create models using inspectdb then edit them manually until they work Commented Aug 5, 2015 at 13:24
  • If the problem is just that inspectdb doesn't recognise the foreign key field, you just have to change the definition in your model, that's all. Commented Aug 5, 2015 at 13:25
  • As @Anentropic said, i'm afraid there is no good alternative to 'inspectdb'. I suggest you to read this article djangobook.com/en/2.0/chapter18.html for clean and good manual correction. Commented Aug 5, 2015 at 13:26
  • I just want to know if there any other better way to do this and If inspectdb is the best way i will definitely go with it.Thanks all. Commented Aug 5, 2015 at 13:34
  • Still i stuck in other problem that i am having two databases and on running inpectdb command it create model.py file with default db. Is there any way so that i can specify another database name in that command. Commented Aug 5, 2015 at 13:37

1 Answer 1

5

You could access data from legacy database using connection.cursor() from django.db module.

If you have two dabases

DATABASES = {
    'default': {
        'NAME': 'new_database',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': '',
        'PASSWORD': ''
    },
    'old': {
        'NAME': 'old_database',
        'ENGINE': 'django.db.backends.mysql',
        'USER': '',
        'PASSWORD': ''
    }
}

...

from django.db import connections
cursor = connections['old'].cursor()
cursor.execute("SELECT...")
cursor.fetchall()

refer to docs:

Executing custom SQL directly

Multiple databases

But if you want to modify data in your old database it is better idea to create models.py file and use it as always. Using inspectdb or not is up to you. For example you cold generate model using inpsectdb in separate temporary project, make dumpdata to create json files and upload data to your active project somehow.

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

3 Comments

thanks @singer. So by using this i need not to create two model.py files?
You dont need to. Just access as raw sql your old data, even in another database
Ok as i dont want to update old database and i just want to use its data so i guess cursors will be best for me. Thanks @singer.

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.