What is the value of self._db by default in Django?
Asked Answered
M

3

11

Please see the following code: user.save(using=self._db) What is the default value of self._db for Django? Does this value default to what I've specified under default for database in my settings.py?

I've found questions on Stack Overflow that say this value will provide a database type to Django, but if I've never set it explicitly, what is it by default?

Mekong answered 27/8, 2019 at 3:27 Comment(1)
Possible duplicate of Do Django Model Managers require using=self._dbKarate
K
35

Django default managers use using parameter to define which database underlying the manager should use for operation. This will optionally use. This is used in case you have multiple databases by which you define which database you need to use for operation.

An example user.save(using=self._db) usually defined as "default" from your database configuration in settings.py. For more info click here

Behind the scene self._db set as None. If user.save(using=None), then it will use default database.

For example, your database configuration is like

DATABASES = {
    'default': {
        'NAME': 'app_data',
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'postgres_user',
        'PASSWORD': '****'
    },
    'new_users': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': '****'
    }
}

Then if you want to use default database then use user.save(using=self._db) If you want to use new_users database then use user.save(using="new_users")

Kaja answered 27/8, 2019 at 3:45 Comment(1)
Nice explanation!Fiester
K
1

Yes, Its default value is specified in settings.py

as

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydatabase', 'USER': 'mydatabaseuser', 'PASSWORD': 'mypassword', 'HOST': '127.0.0.1', 'PORT': '5432', } }

as mentioned here

Karate answered 27/8, 2019 at 3:44 Comment(0)
Y
1

Django default managers use using parameters to define which database underlying the manager should use for operation. This will optionally use. This is used in case you have multiple databases by which you define which database you need to use for operation.

An example user.save(using=self._db) is usually defined as "default" from your database configuration in settings.py.

Behind-the-scene self._db set as None. If user.save(using=None), then it will use the default database.

For example, your database configuration is like

DATABASES = {
    'default': {
        'NAME': 'app_data',
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'postgres_user',
        'PASSWORD': '****'
    },
    'new_users': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': '****'
    }
}

Then if you want to use default database then use user.save(using=self._db) If you want to use new_users database then use user.save(using="new_users")

Yalu answered 28/9, 2022 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.