Skip to content

Instantly share code, notes, and snippets.

@jezdez
Created November 3, 2014 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jezdez/aff7081e24268f870b67 to your computer and use it in GitHub Desktop.
Save jezdez/aff7081e24268f870b67 to your computer and use it in GitHub Desktop.
constance_lazy.py
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