Django Cannot convert float to Decimal. First convert the float to a string -


i have model automatically re sizes image , save original when upload picture.

the problem , when save image @ admin panel, error

cannot convert float decimal. first convert float 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 float string")  exception type: typeerror @ /admin/fruit/pic/add/ exception value: cannot convert float decimal.  first convert float string 

i can't figure why 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 official documentation 9.4.8. decimal faq

q. why isn’t float_to_decimal() routine included in module?

a. there question whether advisable mix binary , decimal floating point. also, use requires care avoid representation issues associated binary floating point:

by way can find answer in given link:

def float_to_decimal(f):     "convert floating point number decimal 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 feature included in python 2.7.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -