Skip to content

Commit e36f23e

Browse files
authored
Fixes: #18038 - Ensure DeviceType._abs_weight is stored as an integer (#18039)
* Coerce _abs_weight to int to prevent disagreement with PositiveBigIntegerField deserialization * Perform coercion in to_grams
1 parent 13bd2ed commit e36f23e

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

netbox/utilities/conversion.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
)
1111

1212

13-
def to_grams(weight, unit):
13+
def to_grams(weight, unit) -> int:
1414
"""
15-
Convert the given weight to kilograms.
15+
Convert the given weight to integer grams.
1616
"""
1717
try:
1818
if weight < 0:
@@ -21,13 +21,13 @@ def to_grams(weight, unit):
2121
raise TypeError(_("Invalid value '{weight}' for weight (must be a number)").format(weight=weight))
2222

2323
if unit == WeightUnitChoices.UNIT_KILOGRAM:
24-
return weight * 1000
24+
return int(weight * 1000)
2525
if unit == WeightUnitChoices.UNIT_GRAM:
26-
return weight
26+
return int(weight)
2727
if unit == WeightUnitChoices.UNIT_POUND:
28-
return weight * Decimal(453.592)
28+
return int(weight * Decimal(453.592))
2929
if unit == WeightUnitChoices.UNIT_OUNCE:
30-
return weight * Decimal(28.3495)
30+
return int(weight * Decimal(28.3495))
3131
raise ValueError(
3232
_("Unknown unit {unit}. Must be one of the following: {valid_units}").format(
3333
unit=unit,

0 commit comments

Comments
 (0)