onconova.core.measures.fields
MeasurementField(verbose_name=None, name=None, measurement=None, measurement_class=None, default_unit=None, unit_choices=None, *args, **kwargs)
¶
Bases: FloatField
A custom Django model field for storing, retrieving, and converting measurement values. MeasurementField extends FloatField to handle values of MeasureBase or BidimensionalMeasure, allowing for unit-aware storage and conversion. It supports specifying the measurement class, default unit, and unit choices, and provides serialization/deserialization methods for database and string representations.
Attributes:
| Name | Type | Description |
|---|---|---|
description |
str
|
Description of the field's purpose. |
empty_strings_allowed |
bool
|
Disallow empty strings as valid values. |
measurement |
type
|
The measurement class (MeasureBase or BidimensionalMeasure). |
default_unit |
str | None
|
The default unit for the measurement. |
unit_choices |
list[str] | tuple[str] | None
|
Allowed unit choices. |
MEASURE_BASES |
tuple
|
Tuple of valid measurement base classes. |
default_error_messages |
dict
|
Error messages for invalid types. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | None
|
Human-readable name for the field. |
None
|
|
str | None
|
Name of the field. |
None
|
|
type | None
|
The measurement class to use. |
None
|
|
str | None
|
Deprecated; name of the measurement class. |
None
|
|
str | None
|
Default unit for the measurement. |
None
|
|
list[str] | tuple[str] | None
|
Allowed unit choices. |
None
|
|
list
|
Additional positional arguments. |
()
|
|
dict
|
Additional keyword arguments. |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If no measurement class is provided or if it is not a valid subclass. |
Source code in onconova/core/measures/fields.py
MEASURE_BASES
class-attribute
instance-attribute
¶
default_error_messages
class-attribute
instance-attribute
¶
default_unit
instance-attribute
¶
description
class-attribute
instance-attribute
¶
empty_strings_allowed
class-attribute
instance-attribute
¶
measurement
instance-attribute
¶
unit_choices
instance-attribute
¶
widget_args
instance-attribute
¶
__new__(**kwargs)
¶
deconstruct()
¶
deserialize_value_from_string(s)
¶
Deserializes a string representation of a measurement value and unit.
The input string should be in the format "value:unit", where value is a float
and unit is a string representing the measurement unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The string to deserialize, expected in the format "value:unit". |
required |
Returns:
| Type | Description |
|---|---|
Measure | None
|
A Measurement object constructed from the value and unit, or None if the input string is not in the expected format. |
Source code in onconova/core/measures/fields.py
from_db_value(value, *args, **kwargs)
¶
Converts a value retrieved from the database into a measurement object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
The value retrieved from the database. |
required |
|
list
|
Additional positional arguments (unused). |
()
|
|
dict
|
Additional keyword arguments (unused). |
{}
|
Returns:
| Type | Description |
|---|---|
Measure | None
|
A measurement object created using the specified measure, value, and the default unit, or None if the value is None. |
Source code in onconova/core/measures/fields.py
get_default_unit()
¶
Returns the default unit for the measurement field.
The method checks for a default unit specified in widget_args["default_unit"].
If not found, it returns the first unit from widget_args["unit_choices"].
If neither is available, it falls back to the standard unit defined in self.measurement.STANDARD_UNIT.
Returns:
| Type | Description |
|---|---|
str
|
The default unit for the measurement field. |
Source code in onconova/core/measures/fields.py
get_prep_value(value)
¶
Prepares the value for database storage.
If the value is None, returns None. If the value is an instance of MEASURE_BASES, converts its 'standard' attribute to a float. Otherwise, delegates preparation to the superclass implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
The value to prepare for storage. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The prepared value suitable for database storage. |
Source code in onconova/core/measures/fields.py
to_python(value)
¶
Converts the input value to a measurement object of the expected type.
Handles various input types:
- If value is None, returns None.
- If value is already an instance of the expected measurement base type, returns it as-is.
- If value is a string, attempts to deserialize it into a measurement object.
- Otherwise, uses the superclass's to_python method to process the value.
If the value is not already a measurement object, constructs one using the default unit, and logs a message indicating the type conversion and the guessed unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Any
|
The input value to be converted. |
required |
Returns:
| Type | Description |
|---|---|
Measure | None
|
An instance of the expected measurement type, or None if input is None. |
Source code in onconova/core/measures/fields.py
value_to_string(obj)
¶
Converts the value obtained from the given object to its string representation.
If the value is not an instance of MEASURE_BASES, it returns the value as is. Otherwise, it returns a string in the format 'value:unit', where 'value' and 'unit' are attributes of the MEASURE_BASES instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
object
|
The object from which to extract the value. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The string representation of the value. |