Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values not working in DATABASES dictionary and values.Value returning Value instance type instead of a str #379

Open
eduardo-boutiquenumerique opened this issue Dec 16, 2023 · 1 comment

Comments

@eduardo-boutiquenumerique

I'm using the lastest pypi version 2.5.

I'm trying to configure my db with separate variables in .env.
I have the following:
DJANGO_DB_NAME=dbname
DJANGO_DB_USER=dbuser
DJANGO_DB_PASSWORD=dbpassword

First of all, if I define them in the DATABASES dictionary they are all None

DATABASES = {
    'default': {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": values.Value(environ_name="DB_NAME"),
        "USER": values.Value(environ_name="DB_USER"),
        "PASSWORD": values.SecretValue(environ_name="DB_PASSWORD"),
        "HOST": "",
        "PORT": "",
    }
}

I get an error saying "ValueError: Value 'DB_PASSWORD' is required to be set as the environment variable 'DJANGO_DB_PASSWORD'" although it's well defined. I have debuging to see if the .env file is loaded and at the end of the "load_dotenv" method is configurations/base.py all the variables are in os.environ.

Also, when I checked the values of NAME and USER, they are "None" instead of correct values.

Second, if I move the values outside of the dictionary as follows:

DB_NAME = values.Value()
DB_USER = values.Value()
DB_PASSWORD = values.SecretValue()

DATABASES = {
    'default': {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": DB_NAME,
        "USER": DB_USER,
        "PASSWORD": DB_PASSWORD,
        "HOST": values.Value(environ_name="DB_HOST", environ_prefix=None),
        "PORT": values.Value(default="5432", environ_name="DB_PORT", environ_prefix=None),
        "OPTIONS": values.DictValue(environ_name="DB_OPTIONS", environ_prefix=None),
    }
}

I get an error in "File "django/db/backends/postgresql/base.py", line 200, in get_connection_params" saying
"TypeError: object of type 'Value' has no len()"
The line in question is "if len(settings_dict["NAME"] or "") > self.ops.max_name_length():"

Again, debugging it, I can verify that type(settings_dict["NAME"]) is "configurations.Value"
The DB_USER and DB_PASSWORD don't seem to cause any issue.

For some unexplained reason, converting it to string as str(DB_NAME) does't work, since it gives me the string "none" as "NAME" in the dictionary.

Third, I tried using the DatabaseURLValue and it works, but when I try to add the "OPTIONS" key to the db dict in order to add the schema info, I get an error saying "*** TypeError: 'DatabaseURLValue' object is not subscriptable"

Here is the code :
DATABASES = values.DatabaseURLValue()
DATABASES["default"]["OPTIONS"] = values.DictValue(environ_name="DB_OPTIONS", environ_prefix=None)

Can you explain all the 3 issues I'm having ?

@eduardo-boutiquenumerique
Copy link
Author

eduardo-boutiquenumerique commented Dec 18, 2023

BTW, I'm using successfully the 1st approach, where I define the variables in the dictionary itself, in a project in python 2.7, django 1.11.29 and django-configraitons 1.0

EDIT: I actually found a difference that in my project where I'm using the 1st approach. I'm not defining an attribute DATABASES, but a property as in

@property
def DATABASES(self):
    return {"default": ...}

Why is it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant