diff --git a/src/wuttaweb/forms/schema.py b/src/wuttaweb/forms/schema.py index bca71ae..5e0364e 100644 --- a/src/wuttaweb/forms/schema.py +++ b/src/wuttaweb/forms/schema.py @@ -357,15 +357,17 @@ class ObjectRef(colander.SchemaType): def serialize(self, node, appstruct): # pylint: disable=empty-docstring """ """ - # nb. normalize to empty option if no object ref, so that - # works as expected + # normalize to empty option if no object ref, so that works as + # expected if self.empty_option and not appstruct: return self.empty_option[0] - if appstruct is colander.null: + # even if there is no empty option, still treat any false-ish + # value as null + if not appstruct: return colander.null - # nb. keep a ref to this for later use + # keep a ref to this for later use node.model_instance = appstruct # serialize to PK as string diff --git a/tests/forms/test_schema.py b/tests/forms/test_schema.py index f2290ef..844cb44 100644 --- a/tests/forms/test_schema.py +++ b/tests/forms/test_schema.py @@ -257,6 +257,8 @@ class TestObjectRef(DataTestCase): typ = mod.ObjectRef(self.request) value = typ.serialize(node, colander.null) self.assertIs(value, colander.null) + value = typ.serialize(node, None) + self.assertIs(value, colander.null) # model instance person = model.Person(full_name="Betty Boop")