Comments (6)
Interesting idea, indeed.
I'd want to see this implemented as a separate settings class, to reduce the chance of slowing down the default implementation. E.g. I could see each of the settings attributes (that are strings) be automatically rendered with format given the current __dict__
of the class in question. That would mean that you couldn't refer to a setting in the string if it wasn't already defined above, but that's something that can be documented.
from django-configurations.
I've implemented something similar using a descriptor too; the problem I had with it though was that it tends to be confusing when dictionaries are involved:
class Base(Settings):
A = 'world'
B = TemplateProperty('hello {A}') # Yay!
C = {
'd': TemplateProperty('hello {A}') # Boo!
}
When you know the implementation, it's obvious why this wouldn't work, but it doesn't quite gel with the user's desired effect which is, essentially, a lazily evaluated string.
from django-configurations.
Yeah, the descriptor implementation seems indeed like a slightly unintuitive API, or at least there isn't a well established convention in Django that would be close to that, making it harder for users to just jump in. Most settings are iterables or otherwise easy to inspect types, even though Django doesn't enforce that. So question is if this app should do that. E.g. I could see the app to iterate over a distinct set of types (iterables, OrderedDict/SortedDict, sets) to "render" them their values before setting the module level variables.
But on the other hand I'm also hesitant to add a full validation system. Do you think that rendering the variables could be instead just an option of a separate Setting
class, e.g. a template=True
option, that would explicitly enable the template rendering? An own Setting
class would also be the place where type specific preparation could happen, e.g. a ListSetting
class that would provide an easy API for modifying the order of its value, for example for the MIDDLEWARE_CLASSES
setting.
Others could there be, too: DictSetting
with an sorted
parameter, as well as the template
of course; the AppSetting
that would act like a DictSetting
but would have a required prefix
parameter, effectively grouping the settings for 3rd party apps.
from django-configurations.
I thought a helper to get the absolute path of the project directory would also be helpful. I need this in every project for template directories, static files and other stuff.
This could be implemented in a separate class with a generated attribute or using a helper method which could be used to assign the value to an attribute of choice.
from django-configurations.
@keimlink I have implemented this in my settings. I have it generate the path from the manage.py
file.
import imp
import os
class RelPath(object):
@property
def SITE_ROOT(self):
return os.path.dirname(imp.find_module('manage')[1])
def rel_path(self, relative_path):
'''
given any path relative to the `SITE_ROOT`, returns the full path
'''
return os.path.normpath(os.path.join(self.SITE_ROOT,
relative_path))
#Example usage
class Media(RelPath):
MEDIA_URL = '/media/'
@property
def MEDIA_ROOT(self):
return super(Media, self).rel_path('tmp/media/')
from django-configurations.
So there was recently some discussion about this in Django core which led to the addition of the BASE_DIR
setting to the project template but no the global settings that are used by configurations as the default values: https://github.com/django/django/blob/25f2acfed0fc110f88abbfffb5c5c62a76670db0/django/conf/project_template/project_name/settings.py#L11-L13
I've also thought more about this kind of feature and would rather not bless one of the patterns for now since there doesn't seem to be a clear set of helpers that would improve configurations. I'm closing this ticket for now but would encourage you to work on patches for specific helpers instead.
from django-configurations.
Related Issues (20)
- Improve Dotenv usage to avoid basic issues HOT 1
- Use the 'furo' theme for Sphinx documentation
- Improve code blocks in documentation
- Release minor version 2.4.1 HOT 3
- 2.4.1: pytest is failing HOT 2
- Replace deprecated imp module with importlib HOT 4
- How can this be used with `django.setup()` HOT 2
- Make magical importing optional
- <frozen importlib._bootstrap>:1049: ImportWarning: ConfigurationImporter.find_spec() not found; falling back to find_module() HOT 4
- "STATICFILES_STORAGE/STORAGES are mutually exclusive" on Django>=4.2
- WSGI application loaded twice unexpectedly
- Release 2.4.2 is not on PyPI HOT 9
- Update Python and Django supported versions
- Add support for Python 3.12 HOT 1
- DeprecationWarning for USE_DEPRECATED_PYTZ, CSRF_COOKIE_MASKED, USE_L10N
- Broken link in `Configuration.load_dotenv` docstring HOT 1
- Add Django 5.0 classifier
- Publish version 2.5.1 on PyPi HOT 2
- Values not working in DATABASES dictionary and values.Value returning Value instance type instead of a str HOT 1
- DOTENV - Using environ_name strange behevior HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-configurations.