DJ_DATABASE_URL in startproject

I love Django, been using it since around .92.

One thing I would love to see added to the startproject template is the use of DJ_DATABASE_URL for the database settings. I suspect it hasn’t been added since it’s a 3rd party library, and if so, that’s OK.

If we do add it, I use the following code in settings to keep the defaults to SQLite easy:

DATABASES = {
    'default': dj_database_url.config(
        default='sqlite:///' + (BASE_DIR / 'db.sqlite3').as_posix(),
        conn_max_age=600,
        conn_health_checks=True,
    )
}

My first post here so forgive me if it’s redundant

Hallo! :slight_smile:
I like Django too/ I have been working with him only 2 or 3 years.
Now, i understand - what to do this thedj_database_url.parse if we have the remote db.

But , if we have the local db/ Why do we need the dj_database_url.conf ?
What is the plus from him?
We can write the code below


# abstract example

try:
    import psycopg2
    conn = psycopg2.connect(DATABASE_URL, connect_timeout=3)
    conn.close()

except Exception as e: # When we have been a conflict of above
    SQLITE_DB_PATH = BASE_DIR / ‘db.sqlite3’
    SQLITE_CONFIG = {
        ‘ENGINE’: ‘django.db.backends.sqlite3’,
        ‘NAME’: str(SQLITE_DB_PATH),
    }

DATABASES = {'default': SQLITE_CONFIG}


If what, my english is not very nice :frowning:

Hi @Tryd0g0lik thanks for responding.

The code I showed could be used in all Django projects. If you don’t have the environment variable “DJ_DATBASE_URL” set, it would default to SQLite. If you do have it set, then you’d need whatever python libraries it requires, for example, the postgres libraries.

I think it makes it easy to use SQLite for local development, but then in deploying to production, I don’t need code like yours or other logic to use my production DB.

The DJ_DATBASE library is built for this

You work and you can understand it. :slight_smile:
i don’t understand. How does it work if they have together (.conf and .parse) ?
If production have two db/ It is the basis db (default) and else db (example postgres). And what? It could be into server.

I too install dj-database-url on most of my Django projects. Having some way of parsing database URIs natively would be quite nice. django-environ is another package which does similar.

For suggesting new features to Django (or at least including an external package), I’d recommend posting your suggestion on the new-feature repo. It’s a good place to voice the suggestion and get community feedback!

This particular suggestion probably makes sense to bundle with Integrate Environment Variable Management (Inspired by django-environ) into Django Core · Issue #61 · django/new-features · GitHub, so you could consider adding your vote / comments there.

Thanks @czue for the redirect to that repo, didn’t know it existed. First time posting to this forum, so thank you very much.

I like SQLite for Dev and Postgres for production, with my code example, I only need to define the DATBASE_URL environment variable in production, otherwise it defaults to SQLite.

hope this helps.

Appreciate the redirect @theorangeone !

@czue thanks again for the redirect to that repo, I’ve added my suggestion here

@jfmatth I understand you in the your question about the environment. Ok.
SQLite and PostgreSQL i voiced as an example. Ok/

I want to know - Where do you use this library -dj_database_url :slight_smile:
Did i understand you correctly - what is it (dj_database_url) used only for develop mode?

OK

I can see the benefit of this library (dj_database_url ) only if i will can write some code.
Example

import os
import dj_database_url
# .... and some import by django 's default 

DEFAULT_SQLITE = f"sqlite:///{BASE_DIR / 'db.sqlite3'}"

def build_postgres_url_from_env():
     name = os.getenv("DB_name", "")
     if not name:
          return False
     user= os.getenv("DB_USER", "")
     password = #.....
     hos = # ....
     port = # ...
     
     # Below, we build him.
     auth = f"{user}:{password}@" if  user and password else ""
     return f"postgres-async://{auth}{host}:{port}/{name}"

# then :
# if we have a working DATABASE_URL it mean what we use it/
# or  we can use the postgresql
# other the sqlite.

# Note: This means, our project has three db and they is to the production mode.
database_url = os.getenv("DATABASE_URL ") or build_postgres_url_from_anv() or DEFAULT_SQLITE


DATABASE = {
     'default":dj_database_url.config(
          default=database_ur,
          conn_max_age=600
     )
}

Ebove, means - We or our user have anything from db and oonly the 4 line/row of options.

The 4 line, it’s

 'default":dj_database_url.config(
          default=database_ur,
          conn_max_age=600
     )

and all/end.

How can i say - This is how SQLAlchemy, when you can use any the type database and you needing insert URL , only.

will be work? :backhand_index_pointing_up:

Oooo! Forgot,

For what is it this age/time ? conn_max_age=600? What is it do?

HI @Tryd0g0lik I think you’re making this too complicated.

In Django, without DJ_DATABASE_URL, I would have to modify the code for DATABASES= settings. With DJ_DATABASE_URL, I just have to set and environment variable DATABASE_URL

The examples on the DJ website don’t do it justice. You only need a simple environment variable to have it setup the DATABASES setting.

For dev with the line I showed, it would be just like not modifying settings.py at all. For other environments, I define a DATABASE_URL environment variable, DJ_DATABASE_URL library picks that up and it works, simple as that.

HTH

:slight_smile: I think, When we watch to the side of dj_database_url - we are pursuing different goals