Django无法将float转换为Decimal。首先将float转换为字符串 [英] Django Cannot convert float to Decimal. First convert the float to a string
问题描述
问题是,当我将图像保存在管理面板,我收到这个错误
无法将float转换为Decimal。首先将浮点数转换为字符串
文件C:\o\mysite\fruit\models.py在save
61. super(Pic,self).save(* args,** kwargs)
在__new__
中的文件C:\Python26\lib\decimal.py 652.首先将浮点数转换为字符串)
异常类型:Type / at / admin / fruit / pic / add /
异常值:无法将float转换为十进制。首先将浮点数转换为字符串
我只是不明白为什么我会收到此错误
我的models.py
class Pic(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(
upload_to =image,
blank = True
)
descrip = models .textField()
ratio = models.DecimalField(decimal_places = 5,max_digits = 10,default = 0)
def __unicode __(self):
return self.descrip
def original(self):
width = self.image.width / float(self.ratio)
height = self.image.height / float(self.ratio)
result =< img src ='/ media / {0}'height ='{1}'width ='{2}'>format(
self.image,高度,宽度)
返回结果
def save(self,* args,** kwargs):
pw = self.image.width
ph = self.image.height
mw = 200
mh = 200
self.ratio = getWH(pw,ph,mw,mh,'r')
super(Pic,self) .save(* args,** kwargs)
if(pw> mw)或(ph> mh):
filename = str(self.image.path)
imageObj = img.open(filename)
ratio = 1
if(pw> mw):
ratio = mw / float(pw)
pw = mw
ph = int(math.floor(float(ph)* ratio )
if(ph> mh):
ratio = ratio *(mh / float(ph))
ph = mh
pw = int(math.floor (ph)* ratio))
width = getWH(pw,ph,mw,mh,'w')
height = getWH(pw,ph,mw,mh,'h' )
imageObj = imageObj.resize((width,height),img.ANTIALIAS)
imageObj.save(filename)
def getWH(pw,ph,mw,mh,result):
ratio = 1
if(pw> mw):
ratio = mw / float(pw)
pw = mw
ph = int(math.floor(float(ph)* ratio))
if(ph> mh):
ratio = ratio *(mh / float(ph))
ph = mh
pw = int(math.floor(float(ph)* ratio))
if result = ='w':
返回pw
elif结果=='h':
返回ph
其他:
返回率
从官方文档 9.4.8。十进制常见问题
Q。为什么float_to_decimal()例程不包括在模块中?
A。有一个问题,是否建议混合二进制
和十进制浮点数。另外,它的使用需要注意避免
与二进制浮点相关的表示问题:
顺便说一句,你可以在给定链接中找到答案:
def float_to_decimal(f):
将浮点数转换为小数,不丢失信息
n,d = f.as_integer_ratio()
分子,分母=十进制(n),十进制(d)
ctx =上下文(prec = 60)
result = ctx.divide(分子,分母)
而ctx.flags [不精确]:
ctx.flags [不精确] = False
ctx.prec * = 2
result = ctx.divide(分子,分母)
返回结果
此功能已经包含在python 2.7中。
I have a model that automatically re sizes the image and save the original also when you upload a picture.
The problem is , when I save the image at the admin panel, I get this error
Cannot convert float to Decimal. First convert the float to a string
File "C:\o\mysite\fruit\models.py" in save
61. super(Pic, self).save(*args, **kwargs)
File "C:\Python26\lib\decimal.py" in __new__
652. "First convert the float to a string")
Exception Type: TypeError at /admin/fruit/pic/add/
Exception Value: Cannot convert float to Decimal. First convert the float to a string
I just can't figure why do I get this error
My models.py
class Pic(models.Model):
user = models.ForeignKey(User)
image = models.ImageField(
upload_to="image",
blank=True
)
descrip = models.TextField()
ratio = models.DecimalField(decimal_places=5,max_digits=10,default=0)
def __unicode__(self):
return self.descrip
def original(self):
width = self.image.width / float(self.ratio)
height = self.image.height / float(self.ratio)
result = "<img src='/media/{0}' height='{1}' width='{2}'>".format(
self.image, height, width)
return result
def save(self, *args, **kwargs):
pw = self.image.width
ph = self.image.height
mw = 200
mh = 200
self.ratio = getWH(pw, ph, mw, mh, 'r')
super(Pic, self).save(*args, **kwargs)
if (pw > mw) or (ph > mh):
filename = str(self.image.path)
imageObj = img.open(filename)
ratio = 1
if (pw > mw):
ratio = mw / float(pw)
pw = mw
ph = int(math.floor(float(ph)* ratio))
if ( ph > mh):
ratio = ratio * ( mh /float(ph))
ph = mh
pw = int(math.floor(float(ph)* ratio))
width = getWH(pw, ph, mw, mh, 'w')
height = getWH(pw, ph, mw, mh, 'h')
imageObj = imageObj.resize((width, height),img.ANTIALIAS)
imageObj.save(filename)
def getWH(pw, ph, mw, mh, result):
ratio = 1
if (pw > mw):
ratio = mw / float(pw)
pw = mw
ph = int(math.floor(float(ph)* ratio))
if ( ph > mh):
ratio = ratio * ( mh /float(ph))
ph = mh
pw = int(math.floor(float(ph)* ratio))
if result == 'w':
return pw
elif result == 'h':
return ph
else:
return ratio
As from official documentation 9.4.8. Decimal FAQ
Q. Why isn’t the float_to_decimal() routine included in the module?
A. There is some question about whether it is advisable to mix binary and decimal floating point. Also, its use requires some care to avoid the representation issues associated with binary floating point:
By the way you can find an answer in the given link:
def float_to_decimal(f):
"Convert a floating point number to a Decimal with no loss of information"
n, d = f.as_integer_ratio()
numerator, denominator = Decimal(n), Decimal(d)
ctx = Context(prec=60)
result = ctx.divide(numerator, denominator)
while ctx.flags[Inexact]:
ctx.flags[Inexact] = False
ctx.prec *= 2
result = ctx.divide(numerator, denominator)
return result
And also this feature already included in python 2.7.
这篇关于Django无法将float转换为Decimal。首先将float转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!