title: Django Cannot convert float to Decimal. First convert the float to a string
body: <p>I have a model that automatically re sizes the image and save the original also when you upload a picture.</p>
  
  <p>The problem is , when I save the image at the admin panel, I get this error </p>
  
  <p>Cannot convert float to Decimal.  First convert the float to a string</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I just can't figure why do I get this error</p>
  
  <p>My models.py</p>
  
  <pre><code>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 = "&lt;img src='/media/{0}' height='{1}' width='{2}'&gt;".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 &gt; mw) or (ph &gt; mh):
              filename = str(self.image.path)
              imageObj = img.open(filename)
  
              ratio = 1
  
              if (pw &gt; mw):
                  ratio = mw / float(pw)
                  pw = mw
                  ph = int(math.floor(float(ph)* ratio))
              if ( ph &gt; 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 &gt; mw):
          ratio = mw / float(pw)
          pw = mw
          ph = int(math.floor(float(ph)* ratio))
      if ( ph &gt; 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
  </code></pre>