Try to read data from remote database

145 views
Skip to first unread message

MPY

unread,
Sep 6, 2016, 3:16:29 PM9/6/16
to Django users
Hi everyone , I am new with Django and I try to connect with external database to read the data and show in my view. 

Hi create a new entry in setting.py file with the new database info that I want connect. Something like this.

    'externDB': {
      'ENGINE': 'django.db.backends.mysql',   
      'NAME': '*****',                         
      'USER': '*****',                        
      'PASSWORD': '*****',                
      'HOST': '*****',     
      'PORT': '',                             
   }

In the View file inside my app I create this to read the info from the database

def index(request):
  all_rows = NewModel.objects.using('externDB').all()
  return render_to_response('info/index.html', {'rows': all_rows})

But now I obtain  this error

global name 'NewModel' is not defined

I undestand the error, I have to include the model, something like that "from info.models import NewModel"

but my model in empty, I don't want to save any data from  the model, to my local database, I only want to read then and show in my view.

Any idea , I am missing something with the models, any example that I can read to understand better. 

Thanks in advance!

Tim Graham

unread,
Sep 6, 2016, 3:22:42 PM9/6/16
to Django users
Reply all
Reply to author
Forward
0 new messages