feat: Add generated field - derived_value #613
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Derived value fields are fields that are calculated based on other fields. They are read-only and not visible/editable in the admin interface.
Defining Derived Value Fields
To define a derived value field, use the
derived_valuetype in theCONSTANCE_CONFIGtuple, and provide a callable that returns the desired value as the default. The callable will be invoked with the config object as the only argument, allowing you to access other config values (e.g.,constance.config).Be cautious when using the
configobject to access the current value of the derived field to avoid creating infinite loops.Valid Callables for Derived Value Fields
The following are valid default parameters for derived value fields:
Example
ResultsThis will yield the following values:
The answer is 42The answer is 42The answer is 42Important Note: Derived value fields are not editable in the admin interface, and their value is recalculated every time the config object is accessed. Consequently, derived value fields should never be included in
CONSTANCE_CONFIG_FIELDSETS.