Skip to content

Commit

Permalink
Merge pull request #236 from kevinjqiu/compound-object-set-to-none
Browse files Browse the repository at this point in the history
Set `None` on a field that's a compound type should honour that semantics
  • Loading branch information
bintoro committed Oct 29, 2015
2 parents 9c32443 + 68baa00 commit 2bef299
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion schematics/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def __set__(self, instance, value):
"""
from .types.compound import ModelType
field = instance._fields[self.name]
if not isinstance(value, Model) and isinstance(field, ModelType):
if all((
value is not None,
not isinstance(value, Model),
isinstance(field, ModelType))):
value = field.model_class(value)
instance._data[self.name] = value

Expand Down
15 changes: 15 additions & 0 deletions tests/test_model_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ class Player(Model):
assert p.location is None


def test_simple_embedded_model_set_to_none():
class Location(Model):
country_code = StringType()

class Player(Model):
id = IntType()
location = ModelType(Location)

p = Player(dict(id=1))
p.location = None

assert p.id == 1
assert p.location is None


def test_simple_embedded_model_is_none_within_listtype():
class QuestionResources(Model):
type = StringType()
Expand Down

0 comments on commit 2bef299

Please sign in to comment.