Index: django/db/models/fields/related.py
===================================================================
--- django/db/models/fields/related.py	(revision 8779)
+++ django/db/models/fields/related.py	(working copy)
@@ -686,7 +686,12 @@
         setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
         
     def formfield(self, **kwargs):
-        defaults = {'form_class': forms.ModelChoiceField, 'queryset': self.rel.to._default_manager.complex_filter(self.rel.limit_choices_to)}
+        defaults = {
+            'form_class': forms.ModelChoiceField,
+            'queryset': self.rel.to._default_manager.complex_filter(
+                                                    self.rel.limit_choices_to),
+            'to_field_name': self.rel.field_name,
+        }
         defaults.update(kwargs)
         return super(ForeignKey, self).formfield(**defaults)
 
Index: django/forms/models.py
===================================================================
--- django/forms/models.py	(revision 8779)
+++ django/forms/models.py	(working copy)
@@ -460,15 +460,22 @@
         if self.field.cache_choices:
             if self.field.choice_cache is None:
                 self.field.choice_cache = [
-                    (obj.pk, self.field.label_from_instance(obj))
-                    for obj in self.queryset.all()
+                    self.choice(obj) for obj in self.queryset.all()
                 ]
             for choice in self.field.choice_cache:
                 yield choice
         else:
             for obj in self.queryset.all():
-                yield (obj.pk, self.field.label_from_instance(obj))
+                yield self.choice(obj)
 
+    def choice(self, obj):
+        if self.field.to_field_name:
+            key = getattr(obj, self.field.to_field_name)
+        else:
+            key = obj.pk
+        return (key, self.field.label_from_instance(obj))
+
+
 class ModelChoiceField(ChoiceField):
     """A ChoiceField whose choices are a model QuerySet."""
     # This class is a subclass of ChoiceField for purity, but it doesn't
@@ -480,7 +487,7 @@
 
     def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
                  required=True, widget=None, label=None, initial=None,
-                 help_text=None, *args, **kwargs):
+                 help_text=None, to_field_name=None, *args, **kwargs):
         self.empty_label = empty_label
         self.cache_choices = cache_choices
 
@@ -490,6 +497,7 @@
                        *args, **kwargs)
         self.queryset = queryset
         self.choice_cache = None
+        self.to_field_name = to_field_name
 
     def _get_queryset(self):
         return self._queryset
@@ -532,7 +540,8 @@
         if value in EMPTY_VALUES:
             return None
         try:
-            value = self.queryset.get(pk=value)
+            key = self.to_field_name or 'pk'
+            value = self.queryset.get(**{key: value})
         except self.queryset.model.DoesNotExist:
             raise ValidationError(self.error_messages['invalid_choice'])
         return value
Index: tests/modeltests/model_forms/models.py
===================================================================
--- tests/modeltests/model_forms/models.py	(revision 8779)
+++ tests/modeltests/model_forms/models.py	(working copy)
@@ -78,7 +78,7 @@
 class WriterProfile(models.Model):
     writer = models.OneToOneField(Writer, primary_key=True)
     age = models.PositiveIntegerField()
-    
+
     def __unicode__(self):
         return "%s is %s" % (self.writer, self.age)
 
@@ -120,6 +120,14 @@
 class ArticleStatus(models.Model):
     status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
 
+class Inventory(models.Model):
+   barcode = models.PositiveIntegerField(unique=True)
+   parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
+   name = models.CharField(blank=False, max_length=20)
+
+   def __unicode__(self):
+      return self.name
+
 __test__ = {'API_TESTS': """
 >>> from django import forms
 >>> from django.forms.models import ModelForm, model_to_dict
@@ -1117,7 +1125,7 @@
 Traceback (most recent call last):
 ...
 ValidationError: [u'Enter only digits separated by commas.']
->>> f.clean(',,,,') 
+>>> f.clean(',,,,')
 u',,,,'
 >>> f.clean('1.2')
 Traceback (most recent call last):
@@ -1152,4 +1160,36 @@
 ...
 ValidationError: [u'Select a valid choice. z is not one of the available choices.']
 
+# Foreign keys which use to_field #############################################
+
+>>> apple = Inventory.objects.create(barcode=86, name='Apple')
+>>> pear = Inventory.objects.create(barcode=22, name='Pear')
+>>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
+
+>>> field = ModelChoiceField(Inventory.objects.all(), to_field_name='barcode')
+>>> for choice in field.choices:
+...     print choice
+(u'', u'---------')
+(86, u'Apple')
+(22, u'Pear')
+(87, u'Core')
+
+>>> class InventoryForm(ModelForm):
+...     class Meta:
+...         model = Inventory
+>>> form = InventoryForm(instance=core)
+>>> print form['parent']
+<select name="parent" id="id_parent">
+<option value="">---------</option>
+<option value="86" selected="selected">Apple</option>
+<option value="22">Pear</option>
+<option value="87">Core</option>
+</select>
+
+>>> data = model_to_dict(core)
+>>> data['parent'] = '22'
+>>> form = InventoryForm(data=data, instance=core)
+>>> core = form.save()
+>>> core.parent
+<Inventory: Pear>
 """}
Index: tests/regressiontests/admin_widgets/models.py
===================================================================
--- tests/regressiontests/admin_widgets/models.py	(revision 8779)
+++ tests/regressiontests/admin_widgets/models.py	(working copy)
@@ -5,14 +5,14 @@
 
 class Member(models.Model):
     name = models.CharField(max_length=100)
-    
+
     def __unicode__(self):
         return self.name
 
 class Band(models.Model):
     name = models.CharField(max_length=100)
     members = models.ManyToManyField(Member)
-    
+
     def __unicode__(self):
         return self.name
 
@@ -20,10 +20,18 @@
     band = models.ForeignKey(Band)
     name = models.CharField(max_length=100)
     cover_art = models.FileField(upload_to='albums')
-    
+
     def __unicode__(self):
         return self.name
 
+class Inventory(models.Model):
+   barcode = models.PositiveIntegerField(unique=True)
+   parent = models.ForeignKey('self', to_field='barcode', blank=True, null=True)
+   name = models.CharField(blank=False, max_length=20)
+
+   def __unicode__(self):
+      return self.name
+
 __test__ = {'WIDGETS_TESTS': """
 >>> from datetime import datetime
 >>> from django.utils.html import escape, conditional_escape
@@ -84,6 +92,15 @@
 >>> w._has_changed([1, 2], [u'1', u'3'])
 True
 
+# Check that ForeignKeyRawIdWidget works with fields which aren't related to
+# the model's primary key.
+>>> apple = Inventory.objects.create(barcode=86, name='Apple')
+>>> pear = Inventory.objects.create(barcode=22, name='Pear')
+>>> core = Inventory.objects.create(barcode=87, name='Core', parent=apple)
+>>> rel = Inventory._meta.get_field('parent').rel
+>>> w = ForeignKeyRawIdWidget(rel)
+>>> print w.render('test', core.parent_id, attrs={})
+<input type="text" name="test" value="86" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="/admin_media/img/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a>&nbsp;<strong>Apple</strong>
 """ % {
     'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
     'STORAGE_URL': default_storage.url(''),
Index: django/contrib/admin/widgets.py
===================================================================
--- django/contrib/admin/widgets.py	(revision 8779)
+++ django/contrib/admin/widgets.py	(working copy)
@@ -41,20 +41,20 @@
 
 class AdminDateWidget(forms.TextInput):
     class Media:
-        js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js", 
+        js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js",
               settings.ADMIN_MEDIA_PREFIX + "js/admin/DateTimeShortcuts.js")
-        
+
     def __init__(self, attrs={}):
         super(AdminDateWidget, self).__init__(attrs={'class': 'vDateField', 'size': '10'})
 
 class AdminTimeWidget(forms.TextInput):
     class Media:
-        js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js", 
+        js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js",
               settings.ADMIN_MEDIA_PREFIX + "js/admin/DateTimeShortcuts.js")
 
     def __init__(self, attrs={}):
         super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'})
-    
+
 class AdminSplitDateTime(forms.SplitDateTimeWidget):
     """
     A SplitDateTime Widget that has some admin-specific styling.
@@ -86,7 +86,7 @@
     """
     def __init__(self, attrs={}):
         super(AdminFileWidget, self).__init__(attrs)
-        
+
     def render(self, name, value, attrs=None):
         output = []
         if value and hasattr(value, "url"):
@@ -121,11 +121,12 @@
         if value:
             output.append(self.label_for_value(value))
         return mark_safe(u''.join(output))
-    
+
     def label_for_value(self, value):
-        return '&nbsp;<strong>%s</strong>' % \
-            truncate_words(self.rel.to.objects.get(pk=value), 14)
-            
+        key = self.rel.get_related_field().name
+        obj = self.rel.to.objects.get(**{key: value})
+        return '&nbsp;<strong>%s</strong>' % truncate_words(obj, 14)
+
 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
     """
     A Widget for displaying ManyToMany ids in the "raw_id" interface rather than
@@ -133,7 +134,7 @@
     """
     def __init__(self, rel, attrs=None):
         super(ManyToManyRawIdWidget, self).__init__(rel, attrs)
-    
+
     def render(self, name, value, attrs=None):
         attrs['class'] = 'vManyToManyRawIdAdminField'
         if value:
@@ -141,7 +142,7 @@
         else:
             value = ''
         return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
-    
+
     def label_for_value(self, value):
         return ''
 
@@ -152,7 +153,7 @@
         if value:
             return [value]
         return None
-    
+
     def _has_changed(self, initial, data):
         if initial is None:
             initial = []
