setting up django setting.py with mysql database - python

Im using mysql in my windows7 for a year and its working fine. I recently know about django and trying to catch up the tutorials. I'm having a problem to set up the setting.py and I think its on 'NAME' path.
DATABASES =
{
'default':
{
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join('C:/ProgramData/MySQL/MySQL Server
5.7/Data/mysql', 'db.frm'),
'USER': '***',
'PASSWORD':'***'
}
}

You just need to put the name of the database.
Example:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB NAME',
'USER': 'USER NAME',
'PASSWORD':'USER PW',
}
}
With that, it should work.

Try the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'PUT THE DATABASE NAME HERE',
'USER': 'PUT THE USER NAME',
'PASSWORD': 'PUT THE PASSWORD OF THE USERNAME ABOVE',
'HOST': 'localhost or hostname/IP of the database server',
'PORT': PORT NUMBER OF THE SERVER,
}
}
You don't need to put the path of any database file, just specify the hostname, port, username and password - and Django will connect to it

Related

Can I access other MySQL database in Python Django without having access to its models?

I have this connection to MySQL Workbench database. The thing is, the database and tables isn't made by my Django project so I don't have any access to its class models. Is there any way I can add, update or delete in the database tables?
tables: TestTable1, TestTable2, TestTable3
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '1234',
'PORT': 3306,
'HOST': '127.0.0.1',
'OPTIONS': {
'charset': 'utf8mb4',
'use_unicode': True, },
}
}
You can use custom sql directly. Have a look at:
https://docs.djangoproject.com/en/3.2/topics/db/sql/#executing-custom-sql-directly

How to indicate to a specific version of database? [Django] [PostgreSQL]

I using django version 3.0.2.
I'd like to use postgreSQL as my instance db.
And there are two version postgreSQL in server.
After config setting.py DATABASES parameter, and runserver.
It showed error.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '',
}
}
psycopg2.OperationalError: FATAL: password authentication failed for user "django"
I'm sure that the username and password are correct.
How to config pg_path in Django as odoo:
In this case, I can use the specific version of pgsql. And run smoothly.
I solve this by indicating the port of the version I installed.
In this case, I have a version 9.6 which install in 5432 and the other is 5433.
So I just solved this by filling in 5433.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '5433',
}
}

django.db.utils.OperationalError: (1044, "Access denied for user 'someuser'#'localhost' to database '/path/to/Database"')

I have tried dropping all users and databases and re-creating. I have granted all privileges to my user. I have made sure my settings.py includes the correct setup... for the record here is what I have:
DIR_FOR_DB = '/path/to/'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': DIR_FOR_DB+ 'Database',
'USER': 'someuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
I can't think of any reason why it's not letting me in.
Try this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Database',
'USER': 'someuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '8000',
You need to specify a port and not have a path on the database name.

How to add multiple hosts to django database?

The connection URL to my MySQL server is like so
jdbc:mysql://server1:3306,server2:3306,server3:3306/mydb
After I parse and extract the hosts and ports, how can I pass it in Django DATABASES section below?
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'mydb',
'USER': 'user',
'PASSWORD': 'password',
'HOST': 'server1',
'PORT': '3306'
}
}

How to use inspectdb for multiple mysql schemas in django?

This is the DATABASES dictionary in my Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'account',
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306'
}
}
My database has multiple schemas, for example, account, login, notifications etc.
When I do a python manage.py inspectdb, I only get the classes for the tables in the "account" schema, but I want the classes for all the schemas.
I tried doing python manage.py inspectdb --database=login, but I get a django.db.utils.ConnectionDoesNotExist exception
But, if I change the settings to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'account',
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306'
},
'login': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'login',
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306'
},
}
I get the proper classes of the table login on executing python manage.py inspectdb --database=login. But, problem is, if this is supposed to be the procedure, I have to make an option in the DATABASES option, which will become really long, as I have more than 15 schemas, and I am not sure if doing that will be a good thing or not.
So, I want to know what is the right way of doing this.
I am on Django 1.10.5.
Django needs to know how to reach each one of your databases so you won't be able to avoid declaring them in the DATABASES setting. If your only concern is that you will loose readability, you may create a helper function to get rid of repetition.
For example:
def local_db(name):
return {
'ENGINE': 'django.db.backends.mysql',
'NAME': name,
'USER': 'xxxx',
'PASSWORD': 'xxxx',
'HOST': 'localhost',
'PORT': '3306',
}
DATABASES = {
'default': local_db('account'),
'login': local_db('login'),
# Etc.
}
If you want to inspect all databases at once, you may create a custom command to do so.
For example:
from django.conf import settings
from django.core.management.commands.inspectdb import Command as BaseCommand
class Command(BaseCommand):
def handle(self, **options):
for db_name in settings.DATABASES:
options['database'] = db_name
self.stdout.write('# Generated while inspecting database %s\n' % db_name)
super().handle(**options)

Categories