0

In a django app, I have two postgresql databases connected through settings.py: one is default and other is AppDb. AppDb is placed on a remote machine.

I want to query from a 'Courses' model from AppDb using 'using()' and 'Courses' model is not available in default database.

So my query goes like this:

courseInfo = Courses.objects.using('AppDb').filter(cuser_id = 12)

But I am getting NameError for 'Courses'

Can I have a solution for such queries without using routers

escapee
  • 413
  • 1
  • 6
  • 14

1 Answers1

0

If you have an existing database, you still need to create an app and models for that database in order to use the ORM.

To help you create the model classes, you can use the inspectdb management command which will try to guess the models from an existing database and create the models.py for you. Its not perfect, but it will save you some time.

You will still have to make sure the models have a primary key and are written in the correct order (so that foreign keys will work correctly).

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thank you for adding answer. I tried some part of it and works out that I will use cursor.connection method. As creating models and app doesn't make sense for me without having data stored in it. – escapee Jul 21 '14 at 10:39