9
9
from django_mongodb_backend .fields import PolymorphicEmbeddedModelField
10
10
from django_mongodb_backend .models import EmbeddedModel
11
11
12
- from .models import Cat , Dog , Library , Person
12
+ from .models import Bone , Cat , Dog , Library , Mouse , Person
13
13
from .utils import truncate_ms
14
14
15
15
@@ -103,6 +103,7 @@ def setUpTestData(cls):
103
103
pet = Cat (
104
104
name = f"Cat { x } " ,
105
105
weight = f"{ x } .5" ,
106
+ toys = Mouse (manufacturer = f"Maker { x } " ),
106
107
),
107
108
)
108
109
for x in range (6 )
@@ -113,6 +114,7 @@ def setUpTestData(cls):
113
114
pet = Dog (
114
115
name = f"Dog { x } " ,
115
116
barks = x % 2 == 0 ,
117
+ toys = Bone (brand = f"Brand { x } " ),
116
118
),
117
119
)
118
120
for x in range (6 )
@@ -148,6 +150,19 @@ def test_boolean(self):
148
150
[x for i , x in enumerate (self .dog_owners ) if i % 2 == 0 ],
149
151
)
150
152
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
+
151
166
152
167
class InvalidLookupTests (SimpleTestCase ):
153
168
def test_invalid_field (self ):
0 commit comments