diff --git a/django/forms/models.py b/django/forms/models.py
index f343a1c..637c6bf 100644
a
|
b
|
def construct_instance(form, instance, fields=None, exclude=None):
|
50 | 50 | continue |
51 | 51 | # OneToOneField doesn't allow assignment of None. Guard against that |
52 | 52 | # instead of allowing it and throwing an error. |
53 | | if isinstance(f, models.OneToOneField) and cleaned_data[f.name] is None: |
| 53 | if isinstance(f, models.OneToOneField) and not f.null and cleaned_data[f.name] is None: |
54 | 54 | continue |
55 | 55 | # Defer saving file-type fields until after the other fields, so a |
56 | 56 | # callable upload_to can use the values from other fields. |
diff --git a/tests/regressiontests/model_forms_regress/models.py b/tests/regressiontests/model_forms_regress/models.py
index 376586e..4bddda1 100644
a
|
b
|
class CustomFileField(models.FileField):
|
37 | 37 | |
38 | 38 | class CustomFF(models.Model): |
39 | 39 | f = CustomFileField(upload_to='unused', blank=True) |
| 40 | |
| 41 | class Author(models.Model): |
| 42 | publication = models.OneToOneField(Publication, null=True, blank=True) |
| 43 | full_name = models.CharField(max_length=255) |
diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
index 57d5655..5dfbe00 100644
a
|
b
|
from django.forms.models import modelform_factory
|
6 | 6 | from django.conf import settings |
7 | 7 | from django.test import TestCase |
8 | 8 | |
9 | | from models import Person, Triple, FilePathModel, Article, Publication, CustomFF |
| 9 | from models import Person, Triple, FilePathModel, Article, Publication, CustomFF, Author |
10 | 10 | |
11 | 11 | class ModelMultipleChoiceFieldTests(TestCase): |
12 | 12 | |
… |
… |
class ModelChoiceIteratorTests(TestCase):
|
134 | 134 | date_published=date(1991, 8, 22)) |
135 | 135 | f = Form() |
136 | 136 | self.assertEqual(len(f.fields["publications"].choices), 1) |
| 137 | |
| 138 | class OneToOneFieldTests(TestCase): |
| 139 | def test_non_assignment(self): |
| 140 | class AuthorForm(forms.ModelForm): |
| 141 | class Meta: |
| 142 | model = Author |
| 143 | fields = ['publication', 'full_name'] |
| 144 | |
| 145 | publication = Publication.objects.create(title="Pravda", |
| 146 | date_published=date(1991, 8, 22)) |
| 147 | author = Author.objects.create(publication=publication, full_name='John Doe') |
| 148 | form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author) |
| 149 | if form.is_valid(): |
| 150 | self.assertEqual(form.cleaned_data['publication'], None) |
| 151 | author = form.save() |
| 152 | #author object returned from form still retains original publication object |
| 153 | #that's why we need to retreive it from database again |
| 154 | new_author = Author.objects.get(pk=author.pk) |
| 155 | self.assertEqual(new_author.publication, None) |