KeyError: 'DB_NAME' when doing makemigrations in django - django

I store all my secrets and database params in the dev.env file.
I have 3 different settings files - base, dev and prod.
There is an SQLite database in base, and I want to connect to Postgres in dev.
So I upload my secrets with the environment variable in my dev setting file like this:
from dotenv import load_dotenv
load_dotenv(os.environ.get('ENV_CONFIG', ''))
And I override my database settings in dev settings file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['DB_NAME'],
'USER': os.environ['DB_USER'],
'PASSWORD': os.environ['DB_PASS'],
'HOST': os.environ['DB_HOST'],
'PORT': os.environ['DB_PORT'],
}
}
But when I run makemigrations with dev settings file:
./manage.py makemigrations --settings=app.settings.dev
I get an error:
File "/Users/admin/Desktop/Programming/Python/UkranianFunds/src/app/settings/dev.py", line 35, in <module>
'NAME': os.environ['DB_NAME'],
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'DB_NAME'
I checked and my secret with the key DB_NAME clearly appears in the settings file - I printed it successfully. The name of the database is correct.
What are other reasons that cause that?

Your loaded dev.env file does not contain 'DB_NAME' as a key.

I solved it by replacing os.environ['DB_NAME'] to os.environ.get('DB_NAME').
Weird situation for me because the problem occurred only when I did makemigrations.
After I migrated to Postgres, I tried to run the app with os.environ['DB_NAME'] and it worked fine.
So it seems that the KeyError is being raised only during makemigrations.

Related

how to fix raise ImproperlyConfigured("settings.DATABASES is improperly configured. when deploying on railway

hey guys so i'm follwing this guide
https://dev.to/mr_destructive/django-postgresql-deployment-on-railway-app-d54
on how to deploy my django project on railway
i have everything set locally, it working but once i deploy, the app crashes returning this err
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/backends/dummy/base.py", line 20, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
below is my database setting
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ.get('PGHOST'),
'NAME': os.environ.get('PGDATABASE'),
'USERNAME': os.environ.get('PGUSER'),
'PASSWORD': os.environ.get('PGPASSWORD'),
'PORT':os.environ.get('PGPORT')
}
}
by the way, this is working on my localhost, i only get this err when i deploy to railway
can someone pls help out
Use this setting for postgresql:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
REF: https://docs.djangoproject.com/en/4.1/ref/settings/#databases
You need to add the relevant environment variable names to the railway app variable settings. To add variables click + New Variable and it will prompt you to enter the variable name and its value in it. Copy the Database Connection URL from the PostgreSQL service settings and paste in the app variables DATABASE_URL's value
If you think the method isn't working for you, you can also try the second approach.
You can add the database URL as a single string as well, this is also an alternative rather than specifying all the separate fields.
# pip install dj_database_url python-dotenv
import dj_database_url
import os
from dotenv import load_dotenv
load_dotenv(os.path.join(BASE_DIR, '.env'))
DATABASE_URL = os.getenv("DATABASE_URL")
DATABASES = {
"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=1800),
}

KeyError: 'force_color' error while runing inspectdb in Django

I'm trying to execute inspected command with the exclusion of some tables in Django
I've found this question: How do I inspectdb 1 table from database which Contains 1000 tables similar to mine but the problem is when I run the same code i get a strange error
script.py
from django.core.management.commands.inspectdb import Command
from django.conf import settings
from SFP_test.settings import DATABASES
if not settings.configured:
settings.configure()
settings.DATABASES = DATABASES
Command().execute(table_name_filter=lambda table_name: table_name in ('base_table', 'bp_table', ), database='sfp')
error:
Traceback (most recent call last):
File "/Users/user/PycharmProjects/SFP_crud_test/generateapp.py", line 24, in <module>
Command().execute(table_name_filter=lambda table_name: table_name in ('base_table', 'bp_table', ), database='sfp')
File "/Users/user/PycharmProjects/SFP_test/venv/lib/python3.7/site-packages/django/core/management/base.py", line 348, in execute
if options['force_color'] and options['no_color']:
KeyError: 'force_color'
Try the following
python manage.py inspectdb <tableName> --database <DatabaseName> > output.py
output.py will have your table Model
Additionally, ensure to have your database configured in settings.py as following
DatabaseName': {
'NAME': 'DbName',
'ENGINE': 'django.db.backends.postgresql',
'HOST': 'HostName',
'USER': 'user',
'PORT': 9999,
'OPTIONS': {
'sslmode': 'require',
'sslcert': 'cert',
'sslkey': 'key',
}

django-admin dbshell raises django.core.exceptions.ImproperlyConfigured

I know this question has been asked before, but none of them worked for me so far, so I'm going to give it a chance here.
I'm trying to use MySQL as my database in django, but when I modify the settings.py and run the command:
django-admin dbshell
I get the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
What I did:
- I'm running windows 10.
using pipenv, I create fresh virtual environment.
install django.
start new project.
edit the settings.py
in the settings.py I change the DATABASES to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
also pip installed mysqlclient
Weird thing is that when I make migrations, new tables are created, which means that DB works. why doesn't the command
django-admin dbshell
work?
based on django docs, manage.py does same thing as django-admin, and also adds settings to the sys path, therefore manage.py should be used instead of django-admin
https://docs.djangoproject.com/en/2.1/ref/django-admin/

settings.DATABASES is improperly configured

total newbie here, sorry
Mac OSX 10.8 Python 2.7 (installed with homebrew)
PostgreSQL 9.4(installed with homebrew)
psycopg2 2.5 (installed with macports)
Django 1.0.4 (installed via python setup.py install)
I'm using this tutorial, and after starting python manage.py shell I ran
>>> from django.db import connection
>>> cursor = connection.cursor()
and got the following:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
The DATABASES section of my settings.py file looks like this:
DATABASE_ENGINE = 'django.db.backends.postgresql_psycopg2' #postgresql_psycopg2
DATABASE_NAME = 'mydatabase' #mydatabase
DATABASE_USER = 'sarahr6' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
So I can't figure out why it says it's improperly configured?
You need to specify DATABASES dictionary in settings.py:
A dictionary containing the settings for all databases to be used with
Django. It is a nested dictionary whose contents maps database aliases
to a dictionary containing the options for an individual database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'sarahr6',
'PASSWORD': '',
'HOST': '',
'PORT': ''
}
}

Heroku Database Settings Injection - How do I setup my dev django database?

I'm trying to get my local dev django app to work after following these instructions on adding env database settings.
https://devcenter.heroku.com/articles/django-injection
I followed the instructions but get the following error when my app tries to access the local database
Request Method: GET
Request URL: http://localhost:8000
Django Version: 1.4
Exception Type: ImproperlyConfigured
Exception Value:
You need to specify NAME in your Django settings file.
My database settings originally,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db', # Or path to database file if using sqlite3.
'USER': 'foo', # Not used with sqlite3.
'PASSWORD': 'bar', # Not used with sqlite3.
'HOST': 'localhost',
'PORT': '5432',
}
}
the heroku article says to add the following to the settings file
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
how do I get dj_database_url.config to use my my dev settings when the DATABASE_URL is not available in dev?
You can just add your dev settings to the default values like this...
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://foo:bar#localhost:5432/db')}
Use this in your settings.py:
DATABASES = {'default': dj_database_url.config(default=os.environ['DATABASE_URL'])}
and in your .env file have this:
DATABASE_URL=postgres://localhost/yourdbname
when you launch with "foreman start" it will look at the .env file and create all those environment variables, just like running on Heroku itself. Type "heroku config" to confirm that you have a DATABASE_URL set, which you should if you added the postgres database addon.
Just set an environment variable on your operating system and check weither or not it's set. For instance, with a UNIX system:
# In ~/.bash_profile
export LOCAL_DEV=true
# In settings.py
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
if bool(os.environ.get('LOCAL_DEV', False)):
# Override DATABASES['default'] with your local database configuration
Also, if you need to set an environment variable on your heroku space:
heroku config:add MY_VAR='my_value'
I just tried this and here is my code:
import dj_database_url
local_db = 'postgres://django_login:123456#localhost/django_db'
DATABASES = {'default': dj_database_url.config(default=local_db)}
My database name is "django_db", user name is "django_login", password is "123456".
My code can run both in local machine and heroku.
import dj_database_url
DATABASES = {'default':
dj_database_url.config(default='postgres://yourusername:yourpassword#yourhosturl:5432/yourdbname')}
** Replace bold string with your database settings
if you are using local database then replace yourhosturl by localhost