1919def parent_child1_set ():
2020 return [
2121 (p .name , [c .name for c in p .child1_set .all ()])
22- for p in Parent .objects .order_by ( "id" ). prefetch_related ("child1_set" )
22+ for p in Parent .objects .prefetch_related ("child1_set" )
2323 ]
2424
2525
@@ -53,14 +53,14 @@ def test_specs_for_derived_models(self):
5353 self .assertNotIn ({"model" : "testapp.child" }, specs )
5454
5555 def test_specs_for_app_models (self ):
56- specs = list (specs_for_app_models ("testapp" ))
56+ specs = list (specs_for_app_models ("testapp" , { "delete_missing" : True } ))
5757
5858 self .assertCountEqual (
5959 specs ,
6060 [
61- {"model" : "testapp.parent" },
62- {"model" : "testapp.child1" },
63- {"model" : "testapp.child2" },
61+ {"model" : "testapp.parent" , "delete_missing" : True },
62+ {"model" : "testapp.child1" , "delete_missing" : True },
63+ {"model" : "testapp.child2" , "delete_missing" : True },
6464 ],
6565 )
6666
@@ -126,7 +126,7 @@ def test_force_insert_simple(self):
126126
127127 def test_force_insert_partial_graph (self ):
128128 p1 = Parent .objects .create (name = "blub-1" )
129- p1 .child1_set .create (name = "blub-1-1" )
129+ c1 = p1 .child1_set .create (name = "blub-1-1" )
130130
131131 p2 = Parent .objects .create (name = "blub-2" )
132132 p2 .child1_set .create (name = "blub-2-1" )
@@ -143,7 +143,11 @@ def test_force_insert_partial_graph(self):
143143 ),
144144 * specs_for_derived_models (
145145 Child ,
146- {"filter" : {"parent__in" : [p1 .pk ]}, "force_insert" : True },
146+ {
147+ "filter" : {"parent__in" : [p1 .pk ]},
148+ "force_insert" : True ,
149+ "delete_missing" : True ,
150+ },
147151 ),
148152 ]
149153
@@ -152,9 +156,12 @@ def test_force_insert_partial_graph(self):
152156
153157 self .assertEqual (
154158 parent_child1_set (),
155- [("blub-1" , ["blub-1-1" , "blub-1-1" ]), ("blub-2" , ["blub-2-1" ])],
159+ [("blub-1" , ["blub-1-1" ]), ("blub-2" , ["blub-2-1" ])],
156160 )
157161
162+ c1_new = p1 .child1_set .get ()
163+ self .assertNotEqual (c1 .pk , c1_new .pk )
164+
158165 def test_force_insert_full_graph (self ):
159166 Parent .objects .create (name = "other" )
160167
@@ -169,15 +176,24 @@ def test_force_insert_full_graph(self):
169176 specs = [
170177 * specs_for_models (
171178 [Parent ],
172- {"filter" : {"pk__in" : [p1 .pk ]}, "force_insert" : True },
179+ {
180+ "filter" : {"pk__in" : [p1 .pk ]},
181+ "force_insert" : True ,
182+ "delete_missing" : False , # Do not remove old items
183+ },
173184 ),
174185 * specs_for_derived_models (
175186 Child ,
176- {"filter" : {"parent__in" : [p1 .pk ]}, "force_insert" : True },
187+ {
188+ "filter" : {"parent__in" : [p1 .pk ]},
189+ "force_insert" : True ,
190+ "delete_missing" : False , # Do not remove old items
191+ },
177192 ),
178193 ]
179194
180195 dump = json .loads (dump_specs (specs ))
196+ # from pprint import pprint; print(); pprint(dump)
181197 load_dump (dump )
182198
183199 self .assertEqual (
0 commit comments