Fixed #26179 -- Removed null assignment check for non-nullable foreign key fields.

This commit is contained in:
ZachLiuGIS 2016-02-10 20:46:57 -05:00 committed by Tim Graham
parent 353aecbf8c
commit 04e13c8913
5 changed files with 32 additions and 42 deletions

View file

@ -717,14 +717,11 @@ class ProxyRelatedModelTest(TestCase):
class TestInitWithNoneArgument(SimpleTestCase):
def test_none_not_allowed(self):
# TaggedItem requires a content_type, initializing with None should
# raise a ValueError.
msg = 'Cannot assign None: "TaggedItem.content_type" does not allow null values'
with self.assertRaisesMessage(ValueError, msg):
TaggedItem(content_object=None)
def test_none_allowed(self):
# AllowsNullGFK doesn't require a content_type, so None argument should
# also be allowed.
AllowsNullGFK(content_object=None)
# TaggedItem requires a content_type but initializing with None should
# be allowed.
TaggedItem(content_object=None)

View file

@ -3,6 +3,7 @@ from copy import deepcopy
from django.core.exceptions import FieldError, MultipleObjectsReturned
from django.db import models, transaction
from django.db.utils import IntegrityError
from django.test import TestCase
from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning
@ -486,19 +487,19 @@ class ManyToOneTests(TestCase):
p = Parent.objects.get(name="Parent")
self.assertIsNone(p.bestchild)
# Assigning None fails: Child.parent is null=False.
with self.assertRaises(ValueError):
setattr(c, "parent", None)
# Assigning None will not fail: Child.parent is null=False.
setattr(c, "parent", None)
# You also can't assign an object of the wrong type here
with self.assertRaises(ValueError):
setattr(c, "parent", First(id=1, second=1))
# Nor can you explicitly assign None to Child.parent during object
# creation (regression for #9649).
with self.assertRaises(ValueError):
Child(name='xyzzy', parent=None)
with self.assertRaises(ValueError):
# You can assign None to Child.parent during object creation.
Child(name='xyzzy', parent=None)
# But when trying to save a Child with parent=None, the database will
# raise IntegrityError.
with self.assertRaises(IntegrityError), transaction.atomic():
Child.objects.create(name='xyzzy', parent=None)
# Creation using keyword argument should cache the related object.

View file

@ -228,9 +228,8 @@ class OneToOneTests(TestCase):
ug_bar.place = None
self.assertIsNone(ug_bar.place)
# Assigning None fails: Place.restaurant is null=False
with self.assertRaises(ValueError):
setattr(p, 'restaurant', None)
# Assigning None will not fail: Place.restaurant is null=False
setattr(p, 'restaurant', None)
# You also can't assign an object of the wrong type here
with self.assertRaises(ValueError):