diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-06 04:51:05 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-06 04:51:05 +0000 |
| commit | 53da1e47942f22a56e761d786ba89d05ca55a224 (patch) | |
| tree | d7845386eddb1ec0eeb7d127e536248f53dc92c5 /tests | |
| parent | b5d4a8ae1b63c35113ee225a15e819adb944b6a1 (diff) | |
Fixed #9649 -- Better error handling in model creation.
Previously, you could explicitly assign None to a non-null ForeignKey
(or other) field when creating the model (Child(parent=None), etc). We
now throw an exception when you do that, which matches the behaviour
when you assign None to the attribute after creation.
Thanks to ales.zoulek@gmail.com and ondrej.kohout@gmail.com for some
analysis of this problem.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9983 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/many_to_one_regress/models.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/tests/regressiontests/many_to_one_regress/models.py b/tests/regressiontests/many_to_one_regress/models.py index 2039bd5ddf..3f35bef62c 100644 --- a/tests/regressiontests/many_to_one_regress/models.py +++ b/tests/regressiontests/many_to_one_regress/models.py @@ -1,5 +1,5 @@ """ -Regression tests for a few FK bugs: #1578, #6886 +Regression tests for a few ForeignKey bugs. """ from django.db import models @@ -106,6 +106,17 @@ Traceback (most recent call last): ... ValueError: Cannot assign "<First: First object>": "Child.parent" must be a "Parent" instance. +# Nor can you explicitly assign None to Child.parent during object creation +# (regression for #9649). +>>> Child(name='xyzzy', parent=None) +Traceback (most recent call last): + ... +ValueError: Cannot assign None: "Child.parent" does not allow null values. +>>> Child.objects.create(name='xyzzy', parent=None) +Traceback (most recent call last): + ... +ValueError: Cannot assign None: "Child.parent" does not allow null values. + # Creation using keyword argument should cache the related object. >>> p = Parent.objects.get(name="Parent") >>> c = Child(parent=p) |
