Skip to content

Commit

Permalink
Fixed #7947 -- Handle the display of OneToOneField in model forms cor…
Browse files Browse the repository at this point in the history
…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
brosner committed Aug 22, 2008
1 parent 640e930 commit 1b8b968
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 4 additions & 1 deletion django/contrib/admin/options.py
Expand Up @@ -128,7 +128,10 @@ def formfield_for_dbfield(self, db_field, **kwargs):
formfield = db_field.formfield(**kwargs)
# Don't wrap raw_id fields. Their add function is in the popup window.
if not db_field.name in self.raw_id_fields:
formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site)
# formfield can be None if it came from a OneToOneField with
# parent_link=True
if formfield is not None:
formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site)
return formfield

# For any other type of field, just call its formfield() method.
Expand Down
6 changes: 5 additions & 1 deletion django/db/models/fields/related.py
Expand Up @@ -744,7 +744,6 @@ class OneToOneField(ForeignKey):
"""
def __init__(self, to, to_field=None, **kwargs):
kwargs['unique'] = True
kwargs['editable'] = False
if 'num_in_admin' not in kwargs:
kwargs['num_in_admin'] = 0
super(OneToOneField, self).__init__(to, to_field, OneToOneRel, **kwargs)
Expand All @@ -754,6 +753,11 @@ def contribute_to_related_class(self, cls, related):
SingleRelatedObjectDescriptor(related))
if not cls._meta.one_to_one_field:
cls._meta.one_to_one_field = self

def formfield(self, **kwargs):
if self.rel.parent_link:
return None
return super(OneToOneField, self).formfield(**kwargs)

class ManyToManyField(RelatedField, Field):
def __init__(self, to, **kwargs):
Expand Down
20 changes: 20 additions & 0 deletions tests/modeltests/model_forms/models.py
Expand Up @@ -54,6 +54,12 @@ def save(self):
def __unicode__(self):
return self.headline

class ImprovedArticle(models.Model):
article = models.OneToOneField(Article)

class ImprovedArticleWithParentLink(models.Model):
article = models.OneToOneField(Article, parent_link=True)

class PhoneNumber(models.Model):
phone = models.PhoneNumberField()
description = models.CharField(max_length=20)
Expand Down Expand Up @@ -773,6 +779,20 @@ def __unicode__(self):
>>> list(f.choices)
[(1L, 'multicategory Entertainment'), (2L, "multicategory It's a test"), (3L, 'multicategory Third'), (4L, 'multicategory Fourth')]
# OneToOneField ###############################################################
>>> class ImprovedArticleForm(ModelForm):
... class Meta:
... model = ImprovedArticle
>>> ImprovedArticleForm.base_fields.keys()
['article']
>>> class ImprovedArticleWithParentLinkForm(ModelForm):
... class Meta:
... model = ImprovedArticleWithParentLink
>>> ImprovedArticleWithParentLinkForm.base_fields.keys()
[]
# PhoneNumberField ############################################################
>>> class PhoneNumberForm(ModelForm):
Expand Down

0 comments on commit 1b8b968

Please sign in to comment.