python - Is there any method in django to use database without creating models.py file? -
i having database data filled in , want use in new django app. way use data of database in django app.actually don't want make changes in old database , want use data. please suggest me better approach this.
while serching found command-inspectdb
can generate model.py file database, issues it does'nt map foreign key in model.py, need rearrange our classes in model.py file , more. searching other alternative.
you access data legacy database using connection.cursor() django.db module.
if have 2 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 docs:
but if want modify data in old database better idea create models.py file , use always. using inspectdb or not you. example cold generate model using inpsectdb in separate temporary project, make dumpdata create json files , upload data active project somehow.
Comments
Post a Comment