Remove decimal prefix from queryset django

Hello,

If I run the following query list(Macro.objects.values_list('value', flat=True)), I get the following output

[Decimal('11.16'), Decimal('7.63'), Decimal('8.53'), Decimal('12.99'), Decimal('15.09'), Decimal('13.03'), Decimal('11.35'), Decimal('9.58'), Decimal('9.29'), Decimal('11.56'), Decimal('10.35'), Decimal('9.23'), Decimal('9.84'), Decimal('13.29'), Decimal('14.09'), Decimal('10.82'), Decimal('8.91'), Decimal('5.21'), Decimal('5.16'), Decimal('6.33'), Decimal('5.78'), Decimal('6.50'), Decimal('6.81'), Decimal('5.04'), Decimal('5.80'), Decimal('4.76'), Decimal('3.86'), Decimal('3.56'), Decimal('4.44'), Decimal('4.55'), Decimal('4.65'), Decimal('5.53'), Decimal('4.28'), Decimal('0.49'), Decimal('0.50'), Decimal('0.48'), Decimal('0.31'), Decimal('0.30'), Decimal('0.38'), Decimal('0.44'), Decimal('0.32')]

How do I remove the decimal prefix from a queryset Django?

I don’t know if it’s important to you:

your queryset:
qset = Macro.objects.values_list('value', flat=True)

If you print single item

qset[x]

or all items

for item in qset:
    print(item)

you don’t see Decimal prefix

But in the extreme you can create a new float list

newlist = [float(i) for i in qset]

The Decimal datatype in Python is the appropriate representation of a database decimal field. If you want to convert it away from type Decimal, you need to decide whether to want to convert it to a string or a float. (Strings can then be accurately re-encoded as Decimal if you need to do math on them, floats aren’t an exact representation, but math operations can be performed on them directly.)
Either way, you would most likely be able to use the Cast function to convert the data to a different type within the query.

In the end, it comes down to what you’re going to be doing with the result set from your query.

1 Like