Excel data not sorting the type decimal.Decimal
I have a view where i export data the numerical data but when i sort the data it is not getting sorted as the excel is not considering the values as numerical data how can i convert them to numerical data in order to display the data in numerical format and make the sorting work.i have a function which gets the data here is how it looks.
def get_output_value(self, key, value, neutral=None):
display = value
if value is None and not user.is_active:
return '-', '-'
if value is None:
return f"${Decimal('.00')}", f"${Decimal('.00')}"
if isinstance(value, Decimal):
return f"${intcomma(value.quantize(Decimal('.00')))}",f"${intcomma(display.quantize(Decimal('.00')))}"
return value, display
you should wrap the data in float or int class types
e.g.
def get_output_value(self, key, value, neutral=None):
display = value
if value is None and not user.is_active:
return 0, 0
if value is None:
return float(f"{Decimal('.00')}"), float(f"{Decimal('.00')}")
if isinstance(value, Decimal):
return float(f"{intcomma(value.quantize(Decimal('.00')))}"), float(f"{intcomma(display.quantize(Decimal('.00')))}")
return float(value), display
PROBABLY THE BETTER WAY:
def get_output_value(self, key, value, neutral=None):
display = value
if value is None and not user.is_active:
return 0, 0
if value is None:
return Decimal('.00'), Decimal('.00')
if isinstance(value, Decimal):
return intcomma(value.quantize(Decimal('.00'))), intcomma(display.quantize(Decimal('.00')))
return float(value), display