Skip to content

Commit f320b9f

Browse files
author
Chris Rossi
authored
Use correct class when deserializing a PolyModel entity. (#186)
Fixes #179.
1 parent 911a6c6 commit f320b9f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/google-cloud-ndb/google/cloud/ndb/model.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,13 @@ def _entity_from_ds_entity(ds_entity, model_class=None):
527527
Returns:
528528
.Model: The deserialized entity.
529529
"""
530-
model_class = model_class or Model._lookup_model(ds_entity.kind)
530+
class_key = ds_entity.get("class")
531+
if class_key:
532+
kind = class_key[-1]
533+
else:
534+
kind = ds_entity.kind
535+
536+
model_class = model_class or Model._lookup_model(kind)
531537
entity = model_class()
532538

533539
# Check if we are dealing with a PolyModel, and if so get correct subclass.

packages/google-cloud-ndb/tests/system/test_crud.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,21 +853,24 @@ class SomeKind(ndb.Expando):
853853
@pytest.mark.usefixtures("client_context")
854854
def test_insert_polymodel(dispose_of):
855855
class Animal(ndb.PolyModel):
856-
pass
856+
one = ndb.StringProperty()
857857

858858
class Feline(Animal):
859-
pass
859+
two = ndb.StringProperty()
860860

861861
class Cat(Feline):
862-
pass
862+
three = ndb.StringProperty()
863863

864-
entity = Cat()
864+
entity = Cat(one="hello", two="dad", three="i'm in jail")
865865
key = entity.put()
866866

867867
retrieved = key.get()
868868

869869
assert isinstance(retrieved, Animal)
870870
assert isinstance(retrieved, Cat)
871+
assert retrieved.one == "hello"
872+
assert retrieved.two == "dad"
873+
assert retrieved.three == "i'm in jail"
871874

872875
dispose_of(key._key)
873876

0 commit comments

Comments
 (0)