views:

38

answers:

2

I'm using Django and MySQL to serve a website. I have my own MySQL server on localhost for my site. Now I have a third-party MySQL database that I need to use to read and write to that is on a remote host. I set up the new database's information in my settings.py file, but a problem occurs when I try to access the remote DB. Because the new DB is not created by me, I have no respective classes in models.py file. Reading from it the regular way of Model.objects.using('remote_db_name').get(pk=0) doesn't work because it throws a NameError.

I was wondering what is the correct way to handle both a local and remote database in Django, especially when the remote database only gives you SELECT and INSERT privileges. All the tutorials I've found online are about multiple databases defined in the same Django site, so the models.py file has all the data needed.

Thanks for any help in advance!

+1  A: 

You have two options:

  1. Use Django's legacy database support to autogenerate models for your existing database.

  2. Skip the Django ORM, and use raw SQL to execute SQL statements against the database.

Ned Batchelder
Thanks for the reply. But if I choose to skip Django ORM and use raw SQL, I still need the classes of the remote DB in my models.py file don't I? I need to execute Model.objects.raw(...), which still requires the class. Or do you mean using raw SQL outside of Django? If so what is the best way to do this?
Jd007
from django.db import connection; c = connection.cursor(); c.execute(SQL)
Ned Batchelder
Does this work even if the remote DB is not using Django? In particular, the DB I am trying to connect to is using PHP. Thank you.
Jd007
PS: I tried connection.cursor(), however it seems that the server cannot connect connect to the remote DB, and after a long wait, it shows the 500 error page. Is there a way to check the connection status, whether it's permission denied or wrong host? Oh and what kind of access permissions do I need for Django to access the remote MySQL DB? Right now I only have MySQL access with SELECT and INSERT permissions. Do I also need SSH permission?Thanks!
Jd007
You say "the db is using PHP," and "it shows the 500 error page." It sounds like you are talking about a web server, not a db server.
Ned Batchelder
No what I meant is the remote server's DB that we are trying to connect to does not use Django for ORM, instead they use PHP. So in their MySQL DB there are no django management tables. And the error I get I suspect is from a time out, from either permission denied or something else. Is there any way I can find out exactly what went wrong in connection.cursor() statement? Thanks!
Jd007
+1  A: 

I had many hard-time moments with Django's legacy support - Django was not designed strictly to support legacy databases. Of course there are some tools/methods (like Ned told above) but I'd rather recommend SQLAlchemy as an alternative for you. It's very fast and it was designed to support any kind of databases no matter if they were created via sqlalchemy nor they were legacy dbs.

Of course if you need all other Django's elements, go for the Ned's solution, but remember that you have to create django core tables in this legacy db, so you'll need CREATE privilege.

bx2