1
0
mirror of https://github.com/django/django.git synced 2025-10-26 07:06:08 +00:00

Fixed #12420. Now that OneToOneField allows assignment of None, stop guarding against it in ModelForms. Thanks, andrewsk.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12545 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Joseph Kocherhans
2010-02-23 20:02:18 +00:00
parent c05de31d75
commit 6ba5fb3728
3 changed files with 40 additions and 5 deletions

View File

@@ -6,7 +6,7 @@ from django.forms.models import modelform_factory
from django.conf import settings
from django.test import TestCase
from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF
from models import Person, RealPerson, Triple, FilePathModel, Article, Publication, CustomFF, Author, Author1
class ModelMultipleChoiceFieldTests(TestCase):
@@ -152,3 +152,35 @@ class ModelClassTests(TestCase):
class NoModelModelForm(forms.ModelForm):
pass
self.assertRaises(ValueError, NoModelModelForm)
class OneToOneFieldTests(TestCase):
def test_assignment_of_none(self):
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ['publication', 'full_name']
publication = Publication.objects.create(title="Pravda",
date_published=date(1991, 8, 22))
author = Author.objects.create(publication=publication, full_name='John Doe')
form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
self.assert_(form.is_valid())
self.assertEqual(form.cleaned_data['publication'], None)
author = form.save()
# author object returned from form still retains original publication object
# that's why we need to retreive it from database again
new_author = Author.objects.get(pk=author.pk)
self.assertEqual(new_author.publication, None)
def test_assignment_of_none_null_false(self):
class AuthorForm(forms.ModelForm):
class Meta:
model = Author1
fields = ['publication', 'full_name']
publication = Publication.objects.create(title="Pravda",
date_published=date(1991, 8, 22))
author = Author1.objects.create(publication=publication, full_name='John Doe')
form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
self.assert_(not form.is_valid())