MCPcopy Index your code
hub / github.com/1Panel-dev/MaxKB / get_dynamics_model

Function get_dynamics_model

apps/common/db/search.py:23–51  ·  view source on GitHub ↗

获取一个动态的django模型 :param attr: 模型字段 :param table_name: 表名 :return: django 模型

(attr: dict, table_name='dynamics')

Source from the content-addressed store, hash-verified

21
22
23def get_dynamics_model(attr: dict, table_name='dynamics'):
24 """
25 获取一个动态的django模型
26 :param attr: 模型字段
27 :param table_name: 表名
28 :return: django 模型
29 """
30 # 创建缓存键,基于属性和表名
31 cache_key = hashlib.md5(f"{table_name}_{str(sorted(attr.items()))}".encode()).hexdigest()
32 # print(f'cache_key: {cache_key}')
33
34 # 如果模型已存在,直接返回缓存的模型
35 if cache_key in _model_cache:
36 return _model_cache[cache_key]
37
38 attributes = {
39 "__module__": "knowledge.models",
40 "Meta": type("Meta", (), {'db_table': table_name}),
41 **attr
42 }
43
44 # 使用唯一的类名避免冲突
45 class_name = f'Dynamics_{cache_key[:8]}'
46 model_class = type(class_name, (models.Model,), attributes)
47
48 # 缓存模型
49 _model_cache[cache_key] = model_class
50
51 return model_class
52
53
54def generate_sql_by_query_dict(queryset_dict: Dict[str, QuerySet], select_string: str,

Callers 15

get_query_setMethod · 0.90
get_query_setMethod · 0.90
get_querysetMethod · 0.90
get_querysetMethod · 0.90
get_query_setMethod · 0.90
get_query_setMethod · 0.90
get_query_setMethod · 0.90
oneMethod · 0.90
get_query_setMethod · 0.90
get_query_setMethod · 0.90

Calls 1

encodeMethod · 0.45

Tested by

no test coverage detected