title: How can I get a Django foreignkey using a TextInput widget to display a name, rather than an id?
body: <p>I have the following (simplified for this example) Django models:</p>
  
  <pre><code>class Ingredient(models.Model):
      name = models.CharField(max_length=100)
  
      def __unicode__(self):
          return self.name
  
  class RecipeIngredient(models.Model):
      quantity = models.DecimalField(max_digits=5, decimal_places=3)
      unit_of_measure = models.ForeignKey(UnitOfMeasure)
      ingredient = models.ForeignKey(Ingredient)
      comment = models.CharField(max_length = 40, blank=True)
  
      def __unicode__(self):
          return self.id
  </code></pre>
  
  <p>And I have the following form:</p>
  
  <pre><code>class RecipeIngredientForm(ModelForm):
  
      class Meta:
          model = RecipeIngredient
  
      def __init__(self, *args, **kwargs):
          super(RecipeIngredientForm, self).__init__(*args, **kwargs)
          self.fields['quantity'].widget = forms.TextInput(attrs={'size':'6'})
          self.fields['ingredient'].widget = forms.TextInput(attrs={'size':'30'})
          self.fields['comment'].widget = forms.TextInput(attrs={'size':'38'})
  </code></pre>
  
  <p>When I view the form, the ingredient is displayed by its id value, not its name.  How can I display the name, rather than the id?</p>
  
  <p>UPDATE</p>
  
  <p>A solution (more elegant ideas still welcome) is to subclass the TextInput widget and use the value to get the Ingredient name:</p>
  
  <pre><code>class IngredientInput(forms.TextInput):
      def render(self, name, value, attrs=None):
          new=Ingredient.objects.get(pk=value).name
          value=new
          if value is None:
              value = ''
          final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
          if value != '':
              # Only add the 'value' attribute if a value is non-empty.
              final_attrs['value'] = force_unicode(self._format_value(value))
          return mark_safe(u'&lt;input%s /&gt;' % flatatt(final_attrs))
  </code></pre>