Django 1.6 and python 3.3 database name setup - django

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
How can I change the name of the database properly ?

Related

TypeError: unsupported operand type(s) for /: 'str' and 'str' django setting.py

when im taking my course of django i got this error i don't know how to fix it here is my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
The tutorial you are following has used a pathlib.Path object for BASE_DIR which supports the / operator for joining paths. You need to either use pathlib or use os.path.join if you use strings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
You have to remove / and + like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR + 'db.sqlite3',
}
}

Django/postgres transaction isolation level setting ignored

I have an issue setting transaction isolation level. I want the most strict serializable, while the default is read committed.
I am using:
Django==1.10.6
psycopg2==2.5.1
Running on heroku.
Based on the documentation:
https://docs.djangoproject.com/en/1.10/ref/databases/#isolation-level
I have the following settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'OPTIONS': {
'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
}
}
if on_heroku:
DATABASES['default'] = dj_database_url.config()
Here's the view code:
#require_http_methods(["POST"])
#transaction.atomic
#login_required()
def api_test_add_one(request):
cursor = connection.cursor()
cursor.execute('SHOW default_transaction_isolation')
logger.info("aa: " + str(cursor.fetchone()))
return HttpResponse("{}", content_type="application/json")
The output is:
aa: (u'read committed',)
I have run different tests simultaneously accessing the same endpoint, incrementing an integer in DB and confirmed that transactions were not isolated.
The OPTIONS belongs inside the database's settings, e.g.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgres',
...
'OPTIONS': {
'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
},
},
}
In your case, you replace DATABASES['default'], so your OPTIONS set there would be lost:
if on_heroku:
DATABASES['default'] = dj_database_url.config()
Instead, you can set OPTIONS after setting DATABASES['default'].
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
}
if on_heroku:
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['OPTIONS'] = {
'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
}

django pick from multiple databases for testing

I have multiple databases defined. This is for test profile, and I want to be able to specify which database to be picked for testing. eg: "python manage.py test -db=mysql"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysql_test',
}
}
I went through the django documentation, but i cant find a clear cut way of doing it. One way of getting around this is setting up environment variables, and define both databases as default. Then use the db based on the database type.
please let me know if there is a much better way of doing this.
thanks
Amal
You can use the TEST attribute of DATABASES:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'TEST': {
'NAME' : 'mysql'
},
},
'mysql': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysql_test',
}
}
but is there a reason you don't want to use the default test database django creates?

How to switch django database before I login in admin?

I face a problem that I design a information system base on django-admin.
But I have several independent system to build.every system's Features are all the same.
So I want to put a texbox to select to switch my database before I login in admin.How can I do that?
for example ,when I want to login in admin,I want to freely switch from db1 to db2 or reverse.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
},
'newData': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db2.sqlite3'),
}
}

Database configuration in Django

How do I use a database connection URL in setting up database config in Django as opposed to using a dictionary?
Instead of using:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'PORT': os.getenv('DB_PORT'),
'HOST': os.getenv('DB_HOST')
}
}
I want to use:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgresql://DB_USER:DB_PASSWORD#localhost:5432/DB_NAME'
}
}
This is how I added a database url to one of my django apps.
First, install this package
pip install dj-database-url
Next, add this line to your settings.py in your django project
...
DATABASES['default'] = dj_database_url.config(default=os.getenv('DATABASE_URI'))
...
In place of os.getenv('DATABASE_URI'), you can also explicitly mention your database url
You can refer here to the github repo for more configurations.