title: Problem rendering a Django form field with different representation
body: <p>I've implemented a custom form field for percentages (a modified version of <a href="http://djangosnippets.org/snippets/1914/" rel="nofollow">Djangosnippet #1914</a>). I'm doing this like everybody else in the world: Displaying a value of 0.25 as 25%. In order to achieve this we need to multiply the value by 100 before we display it and divide it by 100 when we save it (see below).</p>
  
  <p>The save-part (<code>clean</code>) works fine but the problem is with displaying the value (<code>render</code> or rather <code>prepare_value</code>). There are different types of values that get passed to <code>prepare_value</code>. It could be your default (mine is 0) so it would be an <em>int</em> but it could also be a <em>Decimal</em> (most of the time) because after all it's a <em>DecimalField</em>.</p>
  
  <p>The problem arises when the form throws an error and values need to be redisplayed. In this case the form value will be passed to it - which is a <em>unicode</em>. So we don't need to multiply the value by 100 as this has been done before (To be more precise: Since the value can be something different than a <em>Decimal</em> - namely an <em>int</em> or a <em>unicode</em> - we need to multiply it only when it's not a <em>Decimal</em>. That is - a <em>DecimalField</em>). As you can see I've got an ugly workaround using <code>isinstance</code> for that.</p>
  
  <p>I'm beginning to think that I really don't understand what's happening here and I've no idea how I can fix this properly. Any suggestions?</p>
  
  <pre><code>class PercentInput(TextInput):
      def render(self, name, value, attrs=None):
          from django.utils.safestring import mark_safe
          return super(PercentInput, self).render(name, value, attrs) + \
                 mark_safe(u' %')
  
  
  class PercentField(DecimalField):
      widget = PercentInput(attrs={'size': 4, 'maxlength': 5})
  
      default_error_messages = {
          'positive': _(u'Must be a positive number.'),
      }
  
      def prepare_value(self, value):
          if isinstance(value, Decimal):
              value *= 100
          return super(PercentField, self).prepare_value(value)
  
      def clean(self, value):
          value = super(PercentField, self).clean(value)
          if value is None:
              return None
          if value &lt; 0:
              raise ValidationError(self.error_messages['positive'])
          return Decimal('%.4f' % (value / 100))
  </code></pre>