jazzband / dj-database-url

Use Database URLs in your Django Application.
https://pypi.org/project/dj-database-url/
BSD 3-Clause "New" or "Revised" License
1.48k stars 205 forks source link

Reverse functionality #110

Closed ferndot closed 5 years ago

ferndot commented 6 years ago

It would be nice if this package could also parse a Django database dictionary into a valid connection URI.

moar55 commented 5 years ago

Why would this be useful may I ask?

ferndot commented 5 years ago

It is useful for when one needs to connect to the database outside the Django ORM. For instance, my team sometimes writes custom utilities and management commands to perform operations with large quantities of data. One can extract parameters from the database dictionary in Django settings, but it would be much more elegant to easily retrieve a standard connection URL.

This is a bit of an edge case, though.

jacobian commented 5 years ago

Can you retrieve the original database url from os.environ? Or am I missing something?

ferndot commented 5 years ago

@jacobian yes, but that means that one is configuring the database separately from Django and changes would need to be reflected in both places, which can be easy to miss. The benefit of the url function would be that one could simply do parse_db_url(settings.DATABASES["default"]) and then simply consume the configuration already used by the rest of the project.

jacobian commented 5 years ago

that means that one is configuring the database separately from Django

Maybe I'm being dense here -- but isn't that exactly the point of this library? The whole idea here is: you've got some external system that's setting up a database, and passing the info to you in env[DATABASE_URL].

What am I missing?

ferndot commented 5 years ago

What you are saying makes sense. This library makes the assumption that the database connection url is located at DATABASE_URL. In which case it would be trivial to simply use that same env variable elsewhere and this feature is pretty redundant. We can close this.