def validate_loop(doctype, name, lft, rgt):
 if (name in frappe.db.sql_list((u'select name from `tab%s` where lft <= %s and rgt >= %s' % (doctype, u'%s', u'%s')), (lft, rgt))):
  frappe.throw(_(u'Item cannot be added to its own descendents'), NestedSetRecursionError)

def validate_loop ( doctype , name , lft , rgt ) : if ( name in frappe . db . sql_list ( ( u'select name from `tab%s` where lft <= %s and rgt >= %s' % ( doctype , u'%s' , u'%s' ) ) , ( lft , rgt ) ) ) : frappe . throw ( _ ( u'Item cannot be added to its own descendents' ) , NestedSetRecursionError )
check if item not an ancestor .