Created
November 3, 2014 20:32
-
-
Save jezdez/aff7081e24268f870b67 to your computer and use it in GitHub Desktop.
constance_lazy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.utils.functional import lazy | |
import constance.config | |
from constance.admin import FIELDS | |
compatible_types = list(FIELDS.keys()) | |
def config(name, default=None): | |
""" | |
Just a silly wrapper arround the constance's config object. | |
""" | |
return getattr(constance.config, name, default) | |
""" | |
A function to use constance's config object in an environment in which | |
one requires lazy values such a model field parameters. | |
E.g. something that is a pretty stupid idea but should show the risk as well:: | |
class Entry(models.Model): | |
title = models.CharField(max_length=config_lazy('ENTRY_MAX_LENGTH')) | |
.. where ``ENTRY_MAX_LENGTH`` is the name of the constance config value. | |
""" | |
config_lazy = lazy(config, *compatible_types) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment