Django queryset-是否可以在FloatField上进行过滤? [英] Django queryset - Isn’t it possible to filter on a FloatField?

查看:105
本文介绍了Django queryset-是否可以在FloatField上进行过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个非常简单的操作,但是遇到了问题。
我有一个简单的模型:

  class MyModel(models.Model):
date = models。 DateTimeField(null = False)
的值= models.FloatField(null = True)
的时间间隔= models.IntegerField(null = True)

我的目标是获取具有最大价值的MyModel对象。

  #获取值
value = MyModel.objects.filter(date__gte ='2014-05-01',date__lte ='2014-05-31')。aggregate(Max('value'))['value__max' ]

#获取对应于最大值
的对象my_object = MyModel.objects.get(value = value)

此代码向我提出一个错误:不存在匹配的查询 ...
我尝试对值(存在的值)进行硬编码,我也遇到了同样的错误。 / p>

我做错了吗?



谢谢。

解决方案

使用浮点数在Python中表示的方式(Django的 FloatField 映射到Python的 float 和Django的 DecimalField 映射到Python的 decimal ;请参见此处)。



区别在于 float 并不精确,因为它们在计算机上的表示方式是 decimal 类型旨在解决其中一些问题,由此引起的(请查看 docs ,他们对此进行了解释



现在鉴于您尝试通过完全匹配来获取 float 的情况,您可以尝试2种方法:


  1. 更改 value的类型e 上的 MyModel DecimalField (即 value = models.DecimalField(max_digits = 5,decimal_places = 2,null = True)


  2. 如果要将类型保留为 FloatField ,则可以尝试查询一系列值,但是最终可能会得到多个结果(因此 MyModel.objects.get()可能会引发异常,抱怨查询返回的结果超过1个): MyModel.objects.filter(value__range =(1.23,1.24) ) MyModel.objects.filter(value__gte = 1.23,value__lt = 1.24)



I’m trying to do a very simple operation but I get an issue. I have a simple model :

class MyModel(models.Model):
    date = models.DateTimeField(null=False)
    value = models.FloatField(null=True)
    interval = models.IntegerField(null=True)

My goal is to get the MyModel object with the biggest value.

#Get the value
value = MyModel.objects.filter(date__gte=’2014-05-01’, date__lte=’2014-05-31’).aggregate(Max(‘value’))[‘value__max’]

# Get the object corresponding to the max value
my_object = MyModel.objects.get(value=value)

This code raise me an error : "matching query does not exist" … I tried to hardcode a value (that exists), I have the same error.

Am I doing something wrong ? Isn’t it possible to filter on a FloatField ?

Thanks.

解决方案

It is with the way floating point numbers are represented in Python (Django's FloatField maps to Python's float and Django's DecimalField maps to Python's decimal; see here).

The difference is that floats are not precise because of the way they are represented on your computer, the decimal type aims to remedy some of these issues that arise through that (have a look at the docs for it, they explain that very well).

Now given your case of trying to fetch a float via an exact match you can try 2 things:

  1. Change the type of value on MyModel to a DecimalField (i.e. value = models.DecimalField(max_digits=5, decimal_places=2, null=True))

  2. If you want to keep the type as a FloatField you can try to query a range of values, however you might end up with more than 1 result (so MyModel.objects.get() might throw an exception, complaining that the query returned more than 1 result): MyModel.objects.filter(value__range=(1.23, 1.24)) or MyModel.objects.filter(value__gte=1.23, value__lt=1.24)

这篇关于Django queryset-是否可以在FloatField上进行过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆