def get_model_root(model):
 if model._meta.parents:
  parent_model = list(model._meta.parents.items())[0][0]
  return get_model_root(parent_model)
 return model

def get_model_root ( model ) : if model . _meta . parents : parent_model = list ( model . _meta . parents . items ( ) ) [ 0 ] [ 0 ] return get_model_root ( parent_model ) return model
this function finds the root model for any given model .