API Reference
This section provides detailed API documentation for all modules, classes, and functions in django-app-parameter.
Models
The models module contains the core data models for storing parameters, validators, and history.
- exception django_app_parameter.models.ParameterValueTypeError[source]
Bases:
BaseExceptionRaised when a parameter value is of incorrect type
- class django_app_parameter.models.Parameter(*args, **kwargs)[source]
Bases:
ModelBase model for application parameters with typed value support.
Parameters are stored as strings in the database and converted to their appropriate Python types when accessed. Supports encryption, validation, and value history tracking.
The default value type is STR (string).
- ..todo::
Add validate_value methode to validate without setting value
add form field support
-
objects:
ParameterManager= <django_app_parameter.managers.ParameterManager object>
- name
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- slug
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- value_type
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- description
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- value
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- is_global
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- enable_cypher
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- enable_history
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- classmethod from_db(db, field_names, values)[source]
Create instance from database row and convert to appropriate proxy class.
This method is called by Django’s ORM when loading instances from the database. It automatically converts the instance to the correct proxy class based on the value_type field.
- Return type:
- Parameters:
db (str | None)
field_names (Collection[str])
values (Collection[Any])
- get_type()[source]
Return the TYPES value for this parameter.
- Return type:
- Returns:
The type code (e.g., ‘INT’, ‘STR’, etc.). Defaults to ‘STR’.
- set(new_value, auto_cast=False)[source]
Set the parameter value with type validation.
- Parameters:
- Raises:
ParameterValueTypeError – If value is not of expected type.
ValidationError – If value fails validator checks.
- Return type:
- to_dict(decrypt=True)[source]
Export this parameter instance to JSON-compatible dictionary.
- Return type:
ParameterDict_- Returns:
Dictionary with all parameter fields and validators. Note: The value is exported in decrypted form for portability. History entries are NOT exported.
- Parameters:
decrypt (bool)
- from_dict(data, force_encrypt=False)[source]
Update this parameter instance from a dictionary.
- Parameters:
data (
ParameterDict_) – Dictionary containing parameter fields and optionally validators. The ‘slug’ and ‘value_type’ fields are ignored if the instance already exists (has a pk), as they should not be changed. Validators are always processed: if not present in data, existing validators are removed. History entries are NOT imported.force_encrypt (
bool) – If True, the ‘value’ field in data is treated as unencrypted and will be encrypted if ‘enable_cypher’ is True.
- Return type:
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- get_value_type_display(*, field=<django.db.models.fields.CharField: value_type>)
- history
Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager class built by
create_forward_many_to_many_manager()defined below.
- id
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- validators
Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager class built by
create_forward_many_to_many_manager()defined below.
- class django_app_parameter.models.ParameterValidator(*args, **kwargs)[source]
Bases:
ModelStores validator configuration for a Parameter
- parameter
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Child.parentis aForwardManyToOneDescriptorinstance.
- validator_type
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- validator_params
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- get_validator()[source]
Instantiate and return the validator based on type and params.
Supports both built-in Django validators and custom validators defined in DJANGO_APP_PARAMETER[‘validators’] setting.
- Return type:
- Returns:
Callable validator function or instance
- Raises:
ValueError – If validator_type is not found in built-in or custom validators
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- id
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- objects = <django.db.models.manager.Manager object>
- parameter_id
- class django_app_parameter.models.ParameterHistory(*args, **kwargs)[source]
Bases:
ModelStores historical values of a Parameter
- parameter
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Child.parentis aForwardManyToOneDescriptorinstance.
- value
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- modified_at
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- get_next_by_modified_at(*, field=<django.db.models.fields.DateTimeField: modified_at>, is_next=True, **kwargs)
- get_previous_by_modified_at(*, field=<django.db.models.fields.DateTimeField: modified_at>, is_next=False, **kwargs)
- id
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- objects = <django.db.models.manager.Manager object>
- parameter_id
- class django_app_parameter.models.ParameterInt(*args, **kwargs)[source]
Bases:
ParameterProxy model for integer parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterStr(*args, **kwargs)[source]
Bases:
ParameterProxy model for string parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterFloat(*args, **kwargs)[source]
Bases:
ParameterProxy model for float parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterDecimal(*args, **kwargs)[source]
Bases:
ParameterProxy model for Decimal parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterJson(*args, **kwargs)[source]
Bases:
ParameterProxy model for JSON parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterBool(*args, **kwargs)[source]
Bases:
ParameterProxy model for boolean parameters.
- FALSY_VALUES = ['false', '0', 'no', 'off']
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterDate(*args, **kwargs)[source]
Bases:
ParameterProxy model for date parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterDatetime(*args, **kwargs)[source]
Bases:
ParameterProxy model for datetime parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterTime(*args, **kwargs)[source]
Bases:
ParameterProxy model for time parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterUrl(*args, **kwargs)[source]
Bases:
ParameterProxy model for URL parameters with validation.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterEmail(*args, **kwargs)[source]
Bases:
ParameterProxy model for email parameters with validation.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterList(*args, **kwargs)[source]
Bases:
ParameterProxy model for comma-separated list parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterDict(*args, **kwargs)[source]
Bases:
ParameterProxy model for dict parameters (suffixed to avoid conflict with TypedDict).
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterPath(*args, **kwargs)[source]
Bases:
ParameterProxy model for filesystem path parameters.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterDuration(*args, **kwargs)[source]
Bases:
ParameterProxy model for duration parameters stored as seconds.
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- class django_app_parameter.models.ParameterPercentage(*args, **kwargs)[source]
Bases:
ParameterProxy model for percentage parameters (0-100).
- exception DoesNotExist
Bases:
DoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
Parameter Model
- class django_app_parameter.models.Parameter(*args, **kwargs)[source]
Bases:
ModelBase model for application parameters with typed value support.
Parameters are stored as strings in the database and converted to their appropriate Python types when accessed. Supports encryption, validation, and value history tracking.
The default value type is STR (string).
- ..todo::
Add validate_value methode to validate without setting value
add form field support
-
objects:
ParameterManager= <django_app_parameter.managers.ParameterManager object>
- name
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- slug
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- value_type
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- description
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- value
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- is_global
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- enable_cypher
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- enable_history
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- classmethod from_db(db, field_names, values)[source]
Create instance from database row and convert to appropriate proxy class.
This method is called by Django’s ORM when loading instances from the database. It automatically converts the instance to the correct proxy class based on the value_type field.
- Return type:
- Parameters:
db (str | None)
field_names (Collection[str])
values (Collection[Any])
- get_type()[source]
Return the TYPES value for this parameter.
- Return type:
- Returns:
The type code (e.g., ‘INT’, ‘STR’, etc.). Defaults to ‘STR’.
- set(new_value, auto_cast=False)[source]
Set the parameter value with type validation.
- Parameters:
- Raises:
ParameterValueTypeError – If value is not of expected type.
ValidationError – If value fails validator checks.
- Return type:
- to_dict(decrypt=True)[source]
Export this parameter instance to JSON-compatible dictionary.
- Return type:
ParameterDict_- Returns:
Dictionary with all parameter fields and validators. Note: The value is exported in decrypted form for portability. History entries are NOT exported.
- Parameters:
decrypt (bool)
- from_dict(data, force_encrypt=False)[source]
Update this parameter instance from a dictionary.
- Parameters:
data (
ParameterDict_) – Dictionary containing parameter fields and optionally validators. The ‘slug’ and ‘value_type’ fields are ignored if the instance already exists (has a pk), as they should not be changed. Validators are always processed: if not present in data, existing validators are removed. History entries are NOT imported.force_encrypt (
bool) – If True, the ‘value’ field in data is treated as unencrypted and will be encrypted if ‘enable_cypher’ is True.
- Return type:
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- get_value_type_display(*, field=<django.db.models.fields.CharField: value_type>)
- history
Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager class built by
create_forward_many_to_many_manager()defined below.
- id
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- validators
Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager class built by
create_forward_many_to_many_manager()defined below.
ParameterValidator Model
- class django_app_parameter.models.ParameterValidator(*args, **kwargs)[source]
Bases:
ModelStores validator configuration for a Parameter
- parameter
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Child.parentis aForwardManyToOneDescriptorinstance.
- validator_type
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- validator_params
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- get_validator()[source]
Instantiate and return the validator based on type and params.
Supports both built-in Django validators and custom validators defined in DJANGO_APP_PARAMETER[‘validators’] setting.
- Return type:
- Returns:
Callable validator function or instance
- Raises:
ValueError – If validator_type is not found in built-in or custom validators
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- id
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- objects = <django.db.models.manager.Manager object>
- parameter_id
ParameterHistory Model
- class django_app_parameter.models.ParameterHistory(*args, **kwargs)[source]
Bases:
ModelStores historical values of a Parameter
- parameter
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Child.parentis aForwardManyToOneDescriptorinstance.
- value
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- modified_at
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- get_next_by_modified_at(*, field=<django.db.models.fields.DateTimeField: modified_at>, is_next=True, **kwargs)
- get_previous_by_modified_at(*, field=<django.db.models.fields.DateTimeField: modified_at>, is_next=False, **kwargs)
- id
A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.
- objects = <django.db.models.manager.Manager object>
- parameter_id
Admin
The admin module provides Django admin customization for parameter management.
Utilities
The utils module contains helper functions for encryption, slugification, and validator management.
Utility functions for django-app-parameter.
- django_app_parameter.utils.parameter_slugify(content)[source]
Transform content : * slugify (with django’s function) * upperise * replace dash (-) with underscore (_)
- django_app_parameter.utils.get_setting(key, default=None)[source]
Get a value from DJANGO_APP_PARAMETER settings dictionary.
- Parameters:
- Return type:
- Returns:
The setting value or default
Example
>>> get_setting("validators", {}) {'even_number': 'myapp.validators.validate_even_number'}
- django_app_parameter.utils.import_validator(validator_path)[source]
Import a validator from a dotted path string.
- Parameters:
validator_path (
str) – Dotted path to the validator (e.g., ‘myapp.validators.validate_even_number’)- Return type:
- Returns:
The imported validator function or class
- Raises:
ImportError – If the module or validator cannot be imported
AttributeError – If the validator doesn’t exist in the module
Example
>>> validator = import_validator("myapp.validators.validate_even_number") >>> validator(4) # Should not raise >>> validator(3) # Should raise ValidationError
- django_app_parameter.utils.get_validator_from_registry(validator_type, use_cache=True)[source]
Get a validator by type from built-in or custom validators.
This function looks up validators in the following order: 1. Built-in Django validators (from BUILTIN_VALIDATORS) 2. Custom validators from settings (DJANGO_APP_PARAMETER[‘validators’])
- Parameters:
- Return type:
- Returns:
The validator class/function, or None if not found
Example
>>> # Built-in validator >>> validator = get_validator_from_registry("MinValueValidator") >>> validator is MinValueValidator True
>>> # Custom validator (from settings) >>> validator = get_validator_from_registry("even_number") >>> validator.__name__ 'validate_even_number'
- django_app_parameter.utils.get_available_validators()[source]
Get all available validators (built-in + custom) with their display names.
Returns a dictionary mapping validator keys to human-readable names. Built-in validators use their class/function names as display names. Custom validators use their keys as display names.
- Returns:
display_name}
- Return type:
Dictionary of {validator_key
Example
>>> validators = get_available_validators() >>> "MinValueValidator" in validators True >>> "even_number" in validators # If defined in settings True
- django_app_parameter.utils.clear_validator_cache()[source]
Clear the validator import cache.
Useful for testing or when validators are dynamically modified.
- Return type:
- django_app_parameter.utils.get_encryption_key(key=None)[source]
Get the encryption key from Django settings or use provided key.
The key should be stored in settings.DJANGO_APP_PARAMETER[‘encryption_key’] and must be a Fernet-compatible key (32 url-safe base64-encoded bytes).
- Parameters:
key (
Union[str,bytes,None]) – Optional encryption key to use. If provided, this key is used instead of the one from settings. Can be str or bytes.- Return type:
- Returns:
The encryption key as bytes
- Raises:
ImproperlyConfigured – If cryptography is not installed, or if the encryption key is not configured and no key parameter is provided
- django_app_parameter.utils.encrypt_value(value, encryption_key=None)[source]
Encrypt a string value using Fernet symmetric encryption.
- Parameters:
- Return type:
- Returns:
The encrypted value as a string (base64-encoded)
- Raises:
ImproperlyConfigured – If cryptography is not installed or if encryption key is not configured
- django_app_parameter.utils.decrypt_value(encrypted_value, encryption_key=None)[source]
Decrypt a string value using Fernet symmetric encryption.
- Parameters:
- Return type:
- Returns:
The decrypted plaintext string
- Raises:
ImproperlyConfigured – If cryptography is not installed or if encryption key is not configured
cryptography.fernet.InvalidToken – If decryption fails (wrong key or corrupted data)
Context Processors
Template context processor for making global parameters available in templates.
Management Commands
Command-line tools for importing and exporting parameters.
dap_load
Command to import parameters into the database
- param –file:
a json file with all the parameter to be added
- param –no-update:
flag to avoid updating existing parameters
- param –json:
dict containing a new parameter’s values, can’t be use with –file
- class django_app_parameter.management.commands.dap_load.Command(stdout=None, stderr=None, no_color=False, force_color=False)[source]
Bases:
BaseCommand- help = 'Import parameters into the database'
dap_dump
Command to export parameters from the database to a JSON file
- param file:
path to the JSON file to create (required)
- class django_app_parameter.management.commands.dap_dump.Command(stdout=None, stderr=None, no_color=False, force_color=False)[source]
Bases:
BaseCommand- help = 'Export parameters from the database to a JSON file'
dap_rotate_key
Command to rotate encryption key for encrypted parameters.
Two-step process for secure key rotation:
- Step 1: Generate new key and backup old one
python manage.py dap_rotate_key - Backs up current key to dap_backup_key.json - Generates and displays new key - Shows next command to run
- Step 2: Apply rotation with old key
python manage.py dap_rotate_key –old-key <key> - Decrypts with old key (from parameter) - Re-encrypts with new key (from settings)
- param –old-key:
Old encryption key for decryption. When provided, performs step 2.
- param –backup-file:
Path to backup file (default: dap_backup_key.json at project root)
- class django_app_parameter.management.commands.dap_rotate_key.Command(stdout=None, stderr=None, no_color=False, force_color=False)[source]
Bases:
BaseCommand- help = 'Rotate encryption key for encrypted parameters (two-step process)'
AccessParameter Proxy
The AccessParameter class provides convenient attribute-style access to parameters.