user3595632
user3595632

Reputation: 5730

Django: filtering on `DecimalField` doesn't work well?

This is my model:

class Tick(BaseModel):
    .
    .
    trade_coin_volume = models.DecimalField(
        max_digits=15,
        decimal_places=8,
    )
    trade_price = models.DecimalField(
        max_digits=15,
        decimal_places=8,
    )
    .
    .

I print out trade_coin_volume of all Tick objects:

In [9]: for tick in Tick.objects.all():
   ...:     print(tick.trade_coin_volume)
   ...:
0.02120853
0.05000000
0.26628998
0.19354556
0.32299392
0.72599405
0.05955935
0.05354201
0.00767441
0.05101970
0.20967645
0.10000001
0.00500000
0.00899999
0.15274410
0.32104315
0.00300000
0.22695384
0.05000000
0.13894616
0.00631414
0.07967759
0.28592241
0.23765636
0.05777923
0.08883787
0.05000000
0.14535185

As you can see above, there are some 0.05000000 values.

But when I filter Tick objects based on this field,

In [11]: Tick.objects.filter(trade_coin_volume=0.05)
Out[11]: <QuerySet []>

In [12]: Tick.objects.filter(trade_coin_volume=0.05000000)
Out[12]: <QuerySet []>

It doesn't show anything!

Why does it happen?

Upvotes: 5

Views: 2131

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

Django DecimalField represented in Python by Decimal instance. Try this:

from decimal import Decimal
Tick.objects.filter(trade_coin_volume=Decimal('0.05'))

Upvotes: 4

Related Questions