0

I have in my User Profile, one field to define the Database name, that user can connect.

DATABASES = {
    'app_data': {
        'NAME': 'app_data',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'postgres_user',
        'PASSWORD': 's3krit'
    },
    'DBNAME_1': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': 'priv4te'
    }
}

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    dbname = models.CharField(u'Database name', max_length=100, null=True, blank=True)

not connecting to 2 different database only creating in single database

2
  • 2
    Why do you expect this to use two different databases? Commented Aug 5, 2019 at 12:33
  • want to change so Commented Aug 6, 2019 at 6:43

1 Answer 1

0

You can use database of your choice with using keyword at runtime.

UserProfile.objects.using('user_data').all()
Sign up to request clarification or add additional context in comments.

5 Comments

You can create a seperate database connection and write raw sql queries.
how to create seperate database connection??
if you are using postgres then conn = psycopg2.connect(host=host, database=database, user=user, password=password) cur = conn.cursor() then write query as sql = "select * from any_table;" cursor.execute(sql) then data = cursor.fetchall()
I want to create a database dynamically at runtime in mysql or sqlite. And not by using raw sql queries. Pls suggest me?
This answers you in better way stackoverflow.com/questions/6585373/…

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.