本文整理汇总了Python中schema.Schema.get_by_code方法的典型用法代码示例。如果您正苦于以下问题:Python Schema.get_by_code方法的具体用法?Python Schema.get_by_code怎么用?Python Schema.get_by_code使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类schema.Schema
的用法示例。
在下文中一共展示了Schema.get_by_code方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_by_sobject
# 需要导入模块: from schema import Schema [as 别名]
# 或者: from schema.Schema import get_by_code [as 别名]
def get_by_sobject(cls, sobject, related_search_type):
'''Get all by sobject respecting hierarchy'''
project_code = sobject.get_project_code()
schema = Schema.get_by_code(project_code)
if not schema:
return []
sobject_code = sobject.get_code()
foreign_key = sobject.get_foreign_key()
search_type = sobject.get_base_search_type()
related_search_type_obj = SearchType.get(related_search_type)
related_key = related_search_type_obj.get_foreign_key()
# get the instance search type
parent_search_type = schema.get_parent_type(search_type)
instance_type = schema.get_instance_type(search_type, related_search_type)
# get all of the instances
search = Search(instance_type)
search.add_filter(foreign_key, sobject_code)
instances = search.get_sobjects()
# if there is no parent, then return the regular search result
if not parent_search_type:
return instances
# find the parent of the sobject
parent = sobject.get_parent( parent_search_type )
if not parent:
return []
# get the instances from the parent
parent_code = parent.get_code()
parent_foreign_key = parent.get_foreign_key()
# get all of the parent instances
parent_instance_type = schema.get_instance_type(parent_search_type, related_search_type)
search = Search(parent_instance_type)
search.add_filter(parent_foreign_key, parent_code)
parent_instances = search.get_sobjects()
# convert the parent instances into child instances
final_instances = []
for parent_instance in parent_instances:
related_code = parent_instance.get_value(related_key)
for instance in instances:
if instance.get_value(foreign_key) == sobject_code \
and instance.get_value(related_key) == related_code:
break
else:
# dynamically create new instances that do not exist
instance = SearchType.create(instance_type)
for name, value in parent_instance.data.items():
if name == "id":
continue
if name == parent_foreign_key:
continue
if value == None:
continue
instance.set_value(name, value)
instance.set_value(foreign_key, sobject_code)
instance.commit()
final_instances.append(instance)
return final_instances