0

I have two databases in my django application. One is default and another is secondary. When I tried the following code it always returns data from the default database.

from django.db import connection

def my_custom_sql(self):

cursor = connection.cursor()

cursor.execute("SELECT * FROM accounts_account where id=%s", [self.id])

row = cursor.fetchall()

return row

I want to execute the query in second database.

3

1 Answer 1

1

you just need to

from django.db import connections

instead of

from django.db import connection

and use your database alias as below:

cursor = connections['secondry_db'].cursor()
Sign up to request clarification or add additional context in comments.

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.