Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7947 -- Handle the display of OneToOneField in model forms cor…

…rectly. Thanks tyson for the report and original patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8469 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1b8b9686fa8b1ad6ee7f1b47cd3cfd59ab85049e 1 parent 640e930
Brian Rosner authored August 22, 2008
5  django/contrib/admin/options.py
@@ -128,7 +128,10 @@ def formfield_for_dbfield(self, db_field, **kwargs):
128 128
             formfield = db_field.formfield(**kwargs)
129 129
             # Don't wrap raw_id fields. Their add function is in the popup window.
130 130
             if not db_field.name in self.raw_id_fields:
131  
-                formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site)
  131
+                # formfield can be None if it came from a OneToOneField with
  132
+                # parent_link=True
  133
+                if formfield is not None:
  134
+                    formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site)
132 135
             return formfield
133 136
 
134 137
         # For any other type of field, just call its formfield() method.
6  django/db/models/fields/related.py
@@ -744,7 +744,6 @@ class OneToOneField(ForeignKey):
@@ -754,6 +753,11 @@ def contribute_to_related_class(self, cls, related):
20  tests/modeltests/model_forms/models.py
@@ -54,6 +54,12 @@ def save(self):
54 54
     def __unicode__(self):
55 55
         return self.headline
56 56
 
  57
+class ImprovedArticle(models.Model):
  58
+    article = models.OneToOneField(Article)
  59
+
  60
+class ImprovedArticleWithParentLink(models.Model):
  61
+    article = models.OneToOneField(Article, parent_link=True)
  62
+
57 63
 class PhoneNumber(models.Model):
58 64
     phone = models.PhoneNumberField()
59 65
     description = models.CharField(max_length=20)
@@ -773,6 +779,20 @@ def __unicode__(self):
773 779
 >>> list(f.choices)
774 780
 [(1L, 'multicategory Entertainment'), (2L, "multicategory It's a test"), (3L, 'multicategory Third'), (4L, 'multicategory Fourth')]
775 781
 
  782
+# OneToOneField ###############################################################
  783
+
  784
+>>> class ImprovedArticleForm(ModelForm):
  785
+...     class Meta:
  786
+...         model = ImprovedArticle
  787
+>>> ImprovedArticleForm.base_fields.keys()
  788
+['article']
  789
+
  790
+>>> class ImprovedArticleWithParentLinkForm(ModelForm):
  791
+...     class Meta:
  792
+...         model = ImprovedArticleWithParentLink
  793
+>>> ImprovedArticleWithParentLinkForm.base_fields.keys()
  794
+[]
  795
+
776 796
 # PhoneNumberField ############################################################
777 797
 
778 798
 >>> class PhoneNumberForm(ModelForm):

0 notes on commit 1b8b968

Please sign in to comment.
Something went wrong with that request. Please try again.