Ticket #12420: t12420_5.diff

File t12420_5.diff, 3.7 KB (added by Andrii Kurinnyi, 14 years ago)

Updated patch to r12403

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 0039516..e756bf3 100644
    a b def construct_instance(form, instance, fields=None, exclude=None):  
    4848            continue
    4949        if exclude and f.name in exclude:
    5050            continue
    51         # OneToOneField doesn't allow assignment of None. Guard against that
    52         # instead of allowing it and throwing an error.
    53         if isinstance(f, models.OneToOneField) and cleaned_data[f.name] is None:
    54             continue
    5551        # Defer saving file-type fields until after the other fields, so a
    5652        # callable upload_to can use the values from other fields.
    5753        if isinstance(f, models.FileField):
  • tests/regressiontests/model_forms_regress/models.py

    diff --git a/tests/regressiontests/model_forms_regress/models.py b/tests/regressiontests/model_forms_regress/models.py
    index 23ac745..871bb6f 100644
    a b class RealPerson(models.Model):  
    4747        if self.name.lower() == 'anonymous':
    4848            raise ValidationError("Please specify a real name.")
    4949
     50class Author(models.Model):
     51    publication = models.OneToOneField(Publication, null=True, blank=True)
     52    full_name = models.CharField(max_length=255)
     53
     54class Author1(models.Model):
     55    publication = models.OneToOneField(Publication, null=False)
     56    full_name = models.CharField(max_length=255)
  • tests/regressiontests/model_forms_regress/tests.py

    diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
    index 3eeecb2..8680bdc 100644
    a b from django.forms.models import modelform_factory  
    66from django.conf import settings
    77from django.test import TestCase
    88
    9 from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF
     9from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF, Author, Author1
    1010
    1111
    1212class ModelMultipleChoiceFieldTests(TestCase):
    class CustomModelFormSaveMethod(TestCase):  
    147147        self.assertEqual(form.is_valid(), False)
    148148        self.assertEqual(form.errors['__all__'], ['Please specify a real name.'])
    149149
     150class OneToOneFieldTests(TestCase):
     151    def test_assignment_of_none(self):
     152        class AuthorForm(forms.ModelForm):
     153            class Meta:
     154                model = Author
     155                fields = ['publication', 'full_name']
     156
     157        publication = Publication.objects.create(title="Pravda",
     158            date_published=date(1991, 8, 22))
     159        author = Author.objects.create(publication=publication, full_name='John Doe')
     160        form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
     161        self.assert_(form.is_valid())
     162        self.assertEqual(form.cleaned_data['publication'], None)
     163        author = form.save()
     164        #author object returned from form still retains original publication object
     165        #that's why we need to retreive it from database again
     166        new_author = Author.objects.get(pk=author.pk)
     167        self.assertEqual(new_author.publication, None)
     168
     169    def test_assignment_of_none_null_false(self):
     170        class AuthorForm(forms.ModelForm):
     171            class Meta:
     172                model = Author1
     173                fields = ['publication', 'full_name']
     174
     175        publication = Publication.objects.create(title="Pravda",
     176            date_published=date(1991, 8, 22))
     177        author = Author1.objects.create(publication=publication, full_name='John Doe')
     178        form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
     179        self.assert_(not form.is_valid())
Back to Top