I have created a mysql database with Cpanel . And I have some settings for database in the settings.py :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '*****_db',
'USER': '******',
'PASSWORD': '********',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': { 'init_command': 'SET storage_engine=INNODB,character_set_connection=utf8,collation_connection=utf8_unicode_ci'
}
}
}
but the problem is when I try to add a new record in django-admin with some arabic chars , I get this error :
OperationalError at /admin/courses/arguments/add/
(1366, "Incorrect string value: '\\xD8\\xB3\\xDA\\xAF' for column `asiatrad_db`.`courses_arguments`.`name` at row 1")
What is the problem ? Do I need to create a new database with charset on utf-8 ?
The “utf8” encoding only supports three bytes per character. The real UTF-8 encoding, which everybody uses, needs up to four bytes per character. See this article.
So use “utf8mb4” charset instead of “utf8”.
The settings.py should look as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '*****_db',
'USER': '******',
'PASSWORD': '********',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': { 'init_command': 'SET storage_engine=INNODB','charset': 'utf8mb4'}
}
Related
After changing the db from sqlite to postgresql(python manage.py makemigrations)running, I am getting this error.How could i overcome this?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'clinilead_e ',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
In postgres, unlike sqlite you have to create the database.
$ createdb clinilead_e
I'm using a database URL string in my settings like:
DATABASES = {
'default': "mysql://root:#localhost:3306/mydb"
}
When I migrate I get this warning:
MySQL Strict Mode is not set for database connection 'default'
Now my question: How can I combine the two things?
I cannot use the "regular" way to set the database settings with a dictionary because my database url comes from an environment variable.
Thx in advance!
You could update your settings afterwards:
DATABASES['default']['OPTIONS'] = {'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"}
I think this one will help you.
you can pass the options as a query string in the URL
DATABASES = {
'default': dj_database_url.config(default="mssql://USER:PASSWORD#HOST:PORT/NAME?init_command=SET sql_mode='STRICT_TRANS_TABLES'&charset=utf8mb4", conn_max_age=500)
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': 'test123',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
'charset': 'utf8mb4',
}
}
}
Try to give init_command of database options in settings.py
If want know more refer the docs django_mysql.W001: Strict Mode
I am using 2 postgres databases in my django app.
'newpostgre': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre2',
'USER': 'tester',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
},
'newpostgre2': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre3',
'USER': 'tester',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
},
I have a very simple model
class Check1(models.Model):
title = models.CharField(max_length=100)
I had run
python manage.py migrate --database=newpostgre
python manage.py migrate --database=newpostgre2
when I open my new_postgre2(for newpostgre) database in postgre, I can see my Check1 table there.
But in my new_postgre3(for newpostgre2) database in postgre, no Check1 table is there, only those initial migrations are there.
Why I can't see my table in new_postgre3 when migrations have been successfully made?
Because The same object name can be used in different schemas sometimes conflict. The best practice allow many users to use one database without interfering with each other. Use this change second database user and check again. I think it is work.
'newpostgre': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre2',
'USER': 'tester',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
},
'newpostgre2': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'new_postgre3',
'USER': 'tester_different',
'PASSWORD': 'password_different',
'HOST': 'localhost',
'PORT': '',
},
python manage.py migrate --database=newpostgre2
I am getting the error OperationalError: fe_sendauth: no password supplied on my production server but I cannot see why...
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': PROD_DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
pg_hba.conf:
host dbname dbuser localhost md5
If I do psql -d dbname -U dbuser -h localhost and then enter the password at the prompt I can see that it works so IDK why django is not sending the password and IDK where to look from here.
I suspect you're not passing the password correctly. Here's how you debug. After the DATABASES line in settings.py, can you try printing out the dict.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'dbuser',
'PASSWORD': PROD_DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
print DATABASES
Then manage.py runserver as you would.
See if the password is properly passed. Apologies my rep's not enough to comment yet.
For future readers also check the spelling and later cases. All database keys must be in UPPER CASE e.g ENGINE, NAME, USER, PASSWORD, HOST and PORT.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.getenv("DB_NAME", "db_name"),
"USER": os.getenv("DB_USERNAME", "db_user"),
"PASSWORD": os.getenv("DB_PASSWORD", "db#password"),
"HOST": os.getenv("DB_HOST", "localhost"),
"PORT": os.getenv("DB_PORT", "5432")
}
}
Does anyone kno how to use a specific database for a single test in Django?
EDIT: At the moment, i'm using sqlite for testing, but, i want to use a mysql database for a specific test (def test_benchmark...), which will benchmark some sql statements.
Thanks
I don't know of any builtin way to do it - but I didn't look for it neither honestly. One possible (dirty) hack is to check what's in sys.argv in your settings (which is just another Python module) and have an alternate database settings for your particular test, ie:
# settings.py
import os, sys
DATABASES = {
'default': {
'NAME': 'db_name',
'ENGINE': 'your.engine',
'HOST': 'server',
'USER': 'django',
'PASSWORD': 'django',
}
}
if "test" in sys.argv and "your-test-name" in sys.argv:
DATABASES = {
'default': {
'NAME': 'other_db_name',
'ENGINE': 'your.engine',
'HOST': 'server',
'USER': 'django',
'PASSWORD': 'django',
}
}
In Django you set the database in your settings.py file. The configuration should look something like this:
DATABASES = {
'default': {
'NAME': 'db_name',
'ENGINE': 'sql_server.pyodbc',
'HOST': 'server',
'USER': 'django',
'PASSWORD': 'django',
'OPTIONS' : { "use_mars" : False,
"provider" : "SQLNCLI10",
"extra_params" : "MARS Connection=True"},
}
}