99from django_mongodb_backend .fields import PolymorphicEmbeddedModelField
1010from django_mongodb_backend .models import EmbeddedModel
1111
12- from .models import Cat , Dog , Library , Person
12+ from .models import Bone , Cat , Dog , Library , Mouse , Person
1313from .utils import truncate_ms
1414
1515
@@ -103,6 +103,7 @@ def setUpTestData(cls):
103103 pet = Cat (
104104 name = f"Cat { x } " ,
105105 weight = f"{ x } .5" ,
106+ toys = Mouse (manufacturer = f"Maker { x } " ),
106107 ),
107108 )
108109 for x in range (6 )
@@ -113,6 +114,7 @@ def setUpTestData(cls):
113114 pet = Dog (
114115 name = f"Dog { x } " ,
115116 barks = x % 2 == 0 ,
117+ toys = Bone (brand = f"Brand { x } " ),
116118 ),
117119 )
118120 for x in range (6 )
@@ -148,6 +150,19 @@ def test_boolean(self):
148150 [x for i , x in enumerate (self .dog_owners ) if i % 2 == 0 ],
149151 )
150152
153+ def test_nested (self ):
154+ # Cat and Dog both have field toys = PolymorphicEmbeddedModelField(...)
155+ # but with different models. It's possible to query the fields of the
156+ # Dog's toys because it's the first model in Person.pet.
157+ self .assertCountEqual (
158+ Person .objects .filter (pet__toys__brand = "Brand 1" ),
159+ [self .dog_owners [1 ]],
160+ )
161+ # The fields of Cat can't be queried.
162+ msg = "The models of field 'toys' have no field named 'manufacturer'."
163+ with self .assertRaisesMessage (FieldDoesNotExist , msg ):
164+ (Person .objects .filter (pet__toys__manufacturer = "Maker 1" ),)
165+
151166
152167class InvalidLookupTests (SimpleTestCase ):
153168 def test_invalid_field (self ):
0 commit comments