Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query floatfield in Django?

I have a FloatField which can have values like 22.33405, 33.567 etc. Now, I need to query using only upto 2 places after decimal like below

#find all objects with value equal to 22.33
ModelName.objects.filter(field = 22.33)

Is it possible to do so - can I round off or just take first 2 places after decimal?

thanks

like image 652
John Avatar asked Dec 27 '22 21:12

John


1 Answers

try this

ModelName.objects.filter(field__gte=22.33, field__lt=(22.33 + 0.01))

or

ModelName.objects.filter(field__range=(22.33, 22.33 + 0.01))

range lookup

like image 56
beholderrk Avatar answered Jan 04 '23 12:01

beholderrk