You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Temperature units are special in pint as they "are non-multiplicative units" (see Pint docs here). The current implementation in django-pint does not handle these properly and declaring a field with temperature units and attempting to save will raise:
pint.errors.OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Celsius). See https://pint.readthedocs.io/en/stable/user/nonmult.html for guidance.
Solution
Thankfully, I think the solution is pretty simple. In quantityfields.fields, first:
class QuantityFieldMixin(object):
...
def from_db_value(self, value: Any, *args, **kwargs) -> Optional[Quantity]:
if value is None:
return None
# return self.ureg.Quantity(value * getattr(self.ureg, self.base_units))
return self.ureg.Quantity(value, getattr(self.ureg, self.base_units))
and then also,
class QuantityFormFieldMixin(object):
...
def clean(self, value):
...
# val = self.ureg.Quantity(val * getattr(self.ureg, units)).to(self.base_units)
val = self.ureg.Quantity(val, getattr(self.ureg, units)).to(self.base_units)
self.validate(val.magnitude)
self.run_validators(val.magnitude)
return val
As far as I'm aware, this change shouldn't affect any other fields.
The text was updated successfully, but these errors were encountered:
Issue
Temperature units are special in pint as they "are non-multiplicative units" (see Pint docs here). The current implementation in django-pint does not handle these properly and declaring a field with temperature units and attempting to save will raise:
pint.errors.OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Celsius). See https://pint.readthedocs.io/en/stable/user/nonmult.html for guidance.
Solution
Thankfully, I think the solution is pretty simple. In
quantityfields.fields
, first:and then also,
As far as I'm aware, this change shouldn't affect any other fields.
The text was updated successfully, but these errors were encountered: