quantityfield package

Submodules

quantityfield.exceptions module

exception quantityfield.exceptions.PrecisionLoss[source]

Bases: ValueError

quantityfield.fields module

class quantityfield.fields.BigIntegerQuantityField(base_units: str, *args, unit_choices: Optional[List[str]] = None, **kwargs)[source]

Bases: quantityfield.fields.QuantityFieldMixin, django.db.models.fields.BigIntegerField

form_field_class

alias of quantityfield.fields.IntegerQuantityFormField

static to_number_type(value)int
class quantityfield.fields.DecimalQuantityField(base_units: str, *args, unit_choices: Optional[List[str]] = None, verbose_name: Optional[str] = None, name: Optional[str] = None, max_digits: Optional[int] = None, decimal_places: Optional[int] = None, **kwargs)[source]

Bases: quantityfield.fields.QuantityFieldMixin, django.db.models.fields.DecimalField

form_field_class

alias of quantityfield.fields.DecimalQuantityFormField

get_db_prep_save(value, connection)[source]

Get Value that shall be saved to database, make sure it is transformed

to_number_type

alias of decimal.Decimal

to_python(value)pint.quantity.Quantity[source]

Convert the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can’t be converted. Return the converted value. Subclasses should override this.

class quantityfield.fields.DecimalQuantityFormField(base_units: str, *args, unit_choices: Optional[List[str]] = None, **kwargs)[source]

Bases: quantityfield.fields.QuantityFieldMixin, django.forms.fields.DecimalField

to_number_type

alias of decimal.Decimal

class quantityfield.fields.IntegerQuantityField(base_units: str, *args, unit_choices: Optional[List[str]] = None, **kwargs)[source]

Bases: quantityfield.fields.QuantityFieldMixin, django.db.models.fields.IntegerField

form_field_class

alias of quantityfield.fields.IntegerQuantityFormField

static to_number_type(value)int
class quantityfield.fields.IntegerQuantityFormField(*args, **kwargs)[source]

Bases: quantityfield.fields.QuantityFormFieldMixin, django.forms.fields.IntegerField

static to_number_type(value)int
class quantityfield.fields.QuantityField(base_units: str, *args, unit_choices: Optional[List[str]] = None, **kwargs)[source]

Bases: quantityfield.fields.QuantityFieldMixin, django.db.models.fields.FloatField

form_field_class

alias of quantityfield.fields.QuantityFormField

to_number_type

alias of float

class quantityfield.fields.QuantityFieldMixin(base_units: str, *args, unit_choices: Optional[List[str]] = None, **kwargs)[source]

Bases: object

clean(value, model_instance)pint.quantity.Quantity[source]

Convert the value’s type and run validation. Validation errors from to_python() and validate() are propagated. Return the correct value if no error is raised.

This is a copy from djangos implementation but modified so that validators are only checked against the magnitude as otherwise the default database validators will not fail because of comparison errors

deconstruct()[source]
formfield(**kwargs)[source]
from_db_value(value, *args, **kwargs)[source]
get_prep_value(value)[source]
to_number_type = NotImplemented

A Django Model Field that resolves to a pint Quantity object

to_python(value)pint.quantity.Quantity[source]
property units
value_to_string(obj)str[source]
class quantityfield.fields.QuantityFormField(*args, **kwargs)[source]

Bases: quantityfield.fields.QuantityFormFieldMixin, django.forms.fields.FloatField

to_number_type

alias of float

class quantityfield.fields.QuantityFormFieldMixin(*args, **kwargs)[source]

Bases: object

This formfield allows a user to choose which units they wish to use to enter a value, but the value is yielded in the base_units

clean(value)[source]

General idea, first try to extract the correct number like done in the other classes and then follow the same procedure as in the django default field

prepare_value(value)[source]
to_number_type = NotImplemented
quantityfield.fields.raise_precision_error_on_imprecise_int(value)int[source]
quantityfield.fields.raise_validation_error_on_imprecise_int(value)int[source]
quantityfield.fields.safe_to_int(value: Union[float, str, int], exception: Type[Exception])int[source]

Check if a value is an int otherwise warn that it can’t be converted :param value: :param exception: which kind of exception is needed to be raised :return:

quantityfield.helper module

quantityfield.helper.check_matching_unit_dimension(ureg: pint.registry.UnitRegistry, base_units: str, units_to_check: List[str])None[source]

Check if all units_to_check have the same Dimension like the base_units If not :raise DimensionalityError

quantityfield.settings module

quantityfield.units module

quantityfield.widgets module

class quantityfield.widgets.QuantityWidget(*, attrs=None, base_units=None, allowed_types=None)[source]

Bases: django.forms.widgets.MultiWidget

decompress(value)[source]

Return a list of decompressed values for the given compressed value. The given value can be assumed to be valid, but not necessarily non-empty.

get_choices(allowed_types=None)[source]
property media

Media for a multiwidget is the combination of all media of the subwidgets.

Module contents