# quantityfield package

## Submodules

## quantityfield.fields module

class quantityfield.fields.BigIntegerQuantityField(base_units: str, *args, unit_choices: Iterable[str] | None = None, **kwargs)

  Bases: `QuantityFieldMixin`, `BigIntegerField`

  form_field_class

    alias of `IntegerQuantityFormField`

  to_number_type

    alias of `int`

class quantityfield.fields.DecimalQuantityField(base_units: str, *args, unit_choices: list[str] | None = None, verbose_name: str = None, name: str = None, max_digits: int = None, decimal_places: int = None, **kwargs)

  Bases: `QuantityFieldMixin`, `DecimalField`

  form_field_class

    alias of `DecimalQuantityFormField`

  get_db_prep_save(value, connection) → Decimal

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

  to_number_type(x: object) → Decimal

class quantityfield.fields.DecimalQuantityFormField(*args, **kwargs)

  Bases: `QuantityFormFieldMixin`, `DecimalField`

  to_number_type(x: object) → Decimal

class quantityfield.fields.IntegerQuantityField(base_units: str, *args, unit_choices: Iterable[str] | None = None, **kwargs)

  Bases: `QuantityFieldMixin`, `IntegerField`

  form_field_class

    alias of `IntegerQuantityFormField`

  to_number_type

    alias of `int`

class quantityfield.fields.IntegerQuantityFormField(*args, **kwargs)

  Bases: `QuantityFormFieldMixin`, `IntegerField`

  to_number_type

    alias of `int`

class quantityfield.fields.PositiveIntegerQuantityField(base_units: str, *args, unit_choices: Iterable[str] | None = None, **kwargs)

  Bases: `QuantityFieldMixin`, `PositiveIntegerField`

  form_field_class

    alias of `IntegerQuantityFormField`

  to_number_type

    alias of `int`

class quantityfield.fields.QuantityField(base_units: str, *args, unit_choices: Iterable[str] | None = None, **kwargs)

  Bases: `QuantityFieldMixin`, `FloatField`

  form_field_class

    alias of `QuantityFormField`

  to_number_type

    alias of `float`

class quantityfield.fields.QuantityFieldMixin(base_units: str, *args, unit_choices: Iterable[str] | None = None, **kwargs)

  Bases: `object`

  clean(value, model_instance) → Quantity

    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[Sequence[None | bool | str | int | float | complex | datetime] | dict[str, None | bool | str | int | float | complex | datetime]], dict[str, Sequence[None | bool | str | int | float | complex | datetime] | dict[str, None | bool | str | int | float | complex | datetime]]]

    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: Quantity) → Quantity

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

  formfield(**kwargs)
  from_db_value(value: Any, *args, **kwargs) → Quantity | None
  get_db_prep_value(value, connection, prepared=False)

    Convert value to database-compatible format. This is called for both save() operations and filter lookups.

  get_prep_value(value: Any) → int | float | Decimal | None

    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], int | float | Decimal]
  to_python(value) → Quantity | None
  property units: str
  validate: Callable
  value_from_object: Callable[[Any], Any]
  value_to_string(obj) → str

class quantityfield.fields.QuantityFormField(*args, **kwargs)

  Bases: `QuantityFormFieldMixin`, `FloatField`

  to_number_type

    alias of `float`

class quantityfield.fields.QuantityFormFieldMixin(*args, **kwargs)

  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)

    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)
  run_validators: Callable
  to_number_type: Callable[[Any], int | float | Decimal]
  validate: Callable

## quantityfield.helper module

quantityfield.helper.check_matching_unit_dimension(ureg: UnitRegistry, base_units: str, units_to_check: list[str]) → None

  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)

  Bases: `MultiWidget`

  decompress(value)

    This function is called during rendering

    It is responsible to split values for the two widgets

  get_choices(allowed_types=None)
  property media

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

## Module contents