How do I import settings when using DJANGO_SETTINGS_MODULE? - django

I have a Django project and am using the DJANGO_SETTINGS_MODULE for managing each environment settings. I have created different settings files for each environment, however, I'm confused how I can access these settings without having to change the imports in my code. For example, I have this settings directory structure:
stats/
config/
settings/
__init__
base.py
dev.py
prod.py
base.py has all my default settings, include some API keys for third party services. I override these in each environment:
base.py
API_KEY = "default"
dev.py
from base import *
API_KEY = "dev"
prod.py
from base import *
API_KEY = "prod"
When I start up my server, I use
./manage.py runserver --settings=stats.config.settings.dev
But how do I access the API_KEY without having to change "dev" to "prod" every time I deploy? This is how I currently access the API_KEY in my code:
from stats.config.settings.dev import API_KEY
I can't seem to find the proper answer to this anywhere. Thanks!

You access your settings via django.conf.settings, you're not supposed to import/access them directly
from django.conf import settings
settings. API_KEY

Related

Rename Django auth db tables in 3.1.2

I am setting up a new django project and
I would like to rename the three auth tables to include a django_ prefix.
i.e. rename to:
django_auth_group / django_auth_group_permissions / django_auth_permission.
I tried putting the following in __init__.py at the project root from this answer:
from django.contrib.sessions.models import Session
Session._meta.db_table = "django_auth_group"
but was getting the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I'm not sure this is even correct. I think I should be overwriting the model somewhere in my models directory so I tried:
from django.contrib.auth.backends import ModelBackend
class AuthGroup(ModelBackend):
class Meta:
swappable = 'AUTH_GROUP'
db_table = 'django_auth_group'
but this also doesn't seem to create the new table when I re-run my docker container.
Any help would be appreciated. Thanks

Wagtail settings only use .dev

I do not understand how to alternate between production and dev settings. Wagtail docs do not cover it and the only wagtail tutorial I can find mentions it and then completely skips over it. There is a settings file:
--| settings
----| __init__.py
----| base.py
----| dev.py
----| production.py
----| .env
my init file:
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
ENV = os.environ.get('AMSS_ENV')
if ENV == 'dev':
from .dev import *
elif ENV == 'prod':
from .production import *
AMSS_ENV is set to 'prod'. I also have the DJANGO_SETTINGS_MODULE variable set to production in the .env from a different attempt. Does the init file not fire first? is my logic broken? I get no errors and everything works but it loads in dev every time. I've tried so many other things and it just sticks like this. Can someone tell me what am I supposed to do? or where I can look?
It is always useful to check wsgi.py and manage.py to see which settings file they are set to. It is very easy to accidentally serve or run commands with the wrong settings file by forgetting about these two files.

from django.conf import settings NOT loading dev settings

My settings are structured like:
/settings/__init__.py
/settings/base.py
/settings/dev.py
/settings/prod.py
The constant RANDOM_VAR is set in dev.py
When I do the following in e.g. urls.py
from django.conf import settings
print(settings.RANDOM_VAR)
I get
AttributeError: 'Settings' object has no attribute 'RANDOM_VAR'
After further testing I see that all my database settings etc. are loaded from dev.py. But when I want to access my dev.py settings through from django.conf import settings it doesn't work.
I don't want to use from <your_path>.settings import dev, because this would not work on production.
Any ideas?
Your settings structure should be like this,
/settings/__init__.py
/settings/base.py
/settings/dev.py
/settings/prod.py
the you can try something like this
from <your_path>.settings import dev
print(dev.RANDOM_VAR)
Hope this helps you

Access python script from one project to another

Main folder
|-project1
|-project2
I have the above structure for django projects.
When I am in project1 in a script i used os.chdir(to_project2) to project 2
I want to access project2's settings.py and fetch some attributes. Is it possible?
#You need to point your directory.[Ex: project2]
import os
os.chdir(project2 )
cd = os.getcwd()
# print the current directory
print("Current directory:", cwd)
#For Access the Django setting attributes
import django
from django.conf import settings
All of those aforementioned code you may use from one of your python file: something.py

Django Cookiecutter: How to import AUTH_USER_MODEL in config/settings/base.py?

As we know, Django-Cookiecutter has a different setup for settings files. The regular from django.conf import settings doesn't work here.
I want to reference to the custom user model defined in the base.py file in the settings directory. Any ideas?
Below is my project layout:
repository/
config/
settings/
__init__.py
base.py # where the auth_user_model variable is defined
local.py
production.py
test.py
app_dir/
users/
__init__.py
models.py # where the custom user model is stored
I also tried to import the custom user model directly from users/models.py as below:
from users.models import User
But got the following error:
RuntimeError: Model class users.models.User doesn't declare an
explicit app_label and isn't in an application in INSTALLED_APPS.
Tried the following, and it seems to work so far:
from config.settings.base import AUTH_USER_MODEL
from django.contrib.auth import get_user_model
It'll return django user class that you defined in base.py
Edit:
from django.conf.settings import AUTH_USER_MODEL