File tree Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -174,6 +174,11 @@ def is_child_allowed(self, child):
174174 return  False 
175175
176176 child_types  =  self .get_child_types ()
177+ 
178+  # this allows tree validation to occur in the event the child model is not 
179+ # yet created in db (ie. when django admin tries to validate) 
180+  child .pre_save_polymorphic ()
181+ 
177182 return  not  child_types  or  child .polymorphic_ctype_id  in  child_types 
178183
179184 def  validate_move (self , target , position = 'first-child' ):
Original file line number Diff line number Diff line change @@ -145,3 +145,8 @@ class ModelMustBeChildRoot(PolymorphicMPTTModel):
145145
146146class  ModelMustBeChild (ModelMustBeChildRoot ):
147147 can_be_root  =  False 
148+ 
149+ class  ModelRestrictedChildren (Base ):
150+  child_types  =  [
151+  ModelX ,
152+  ]
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ class PolymorphicTests(TestCase):
3535 test_custom_pk() 
3636 test_fix_getattribute() 
3737 test_parent_link_and_related_name() 
38+  test_child_type_validation_in_memory() 
3839
3940 """ 
4041
@@ -227,6 +228,21 @@ def test_node_type_checking(self):
227228 self .assertTrue (grandchild .is_leaf_node ())
228229 self .assertFalse (grandchild .is_root_node ())
229230
231+  def  test_child_type_validation_in_memory (self ):
232+  root_node  =  ModelRestrictedChildren .objects .create (field_b = 'root' )
233+ 
234+  valid_child  =  ModelX (field_b = 'valid_child' , field_x = 'ModelX' , parent = root_node )
235+  valid_child .clean ()
236+ 
237+  with  self .assertRaises (ValidationError ) as  context :
238+  invalid_child  =  ModelY (field_b = 'invalid_child' , field_y = 'ModelY' , parent = root_node )
239+  invalid_child .clean ()
240+ 
241+  self .assertTrue ('a model restricted children does not allow model y as a child!' 
242+  in  context .exception .args [0 ]['parent' ])
243+ 
244+ 
245+ 
230246 def  test_tree_manager (self ):
231247 # Having the tree manager correct is absolutely essential, 
232248 # so our move validation is also triggered. 
                         You can’t perform that action at this time. 
           
                  
0 commit comments