quantityfield package

Submodules

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

name: str
to_number_type

alias of 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) decimal.Decimal[source]

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

name: str
to_number_type

alias of decimal.Decimal

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

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

error_messages: Dict[str, str]
localize: bool
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

name: str
to_number_type

alias of int

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

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

error_messages: Dict[str, str]
localize: bool
to_number_type

alias of 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

name: str
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() Tuple[str, str, Sequence[Union[Sequence[Union[None, bool, str, int, float, complex, datetime.datetime]], Dict[str, Union[None, bool, str, int, float, complex, datetime.datetime]]]], Dict[str, Union[Sequence[Union[None, bool, str, int, float, complex, datetime.datetime]], Dict[str, Union[None, bool, str, int, float, complex, datetime.datetime]]]]][source]

Return enough information to recreate the field as a 4-tuple:

  • The name of the field on the model, if contribute_to_class() has been run.

  • The import path of the field, including the class:e.g. django.db.models.IntegerField This should be the most portable version, so less specific may be better.

  • A list of positional arguments.

  • A dict of keyword arguments.

fix_unit_registry(value: pint.quantity.Quantity) pint.quantity.Quantity[source]

Check if the UnitRegistry from settings is used. If not try to fix it but give a warning.

formfield(**kwargs)[source]
from_db_value(value: Any, *args, **kwargs) Optional[pint.quantity.Quantity][source]
get_prep_value(value: Any) Optional[Union[int, float, decimal.Decimal]][source]

Perform preliminary non-db specific value checks and conversions.

Make sure that we compare/use only values without a unit

name: str
run_validators: Callable

A Django Model Field that resolves to a pint Quantity object

to_number_type: Callable[[Any], Union[int, float, decimal.Decimal]]
to_python(value) Optional[pint.quantity.Quantity][source]
property units: str
validate: Callable
value_from_object: Callable[[Any], Any]
value_to_string(obj) str[source]
class quantityfield.fields.QuantityFormField(*args, **kwargs)[source]

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

error_messages: Dict[str, str]
localize: bool
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

empty_values: Sequence[Any]
error_messages: Dict[str, str]
localize: bool
prepare_value(value)[source]
run_validators: Callable
to_number_type: Callable[[Any], Union[int, float, decimal.Decimal]]
validate: Callable

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