0

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!

2 Answers 2

1

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.

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

6 Comments

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?
from django.db import connection; c = connection.cursor(); c.execute(SQL)
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.
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!
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.
|
1

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.

Comments

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.