Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion apps/common/db/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@date:2023/10/7 18:20
@desc:
"""
import hashlib
from typing import Dict, Any

from django.db import DEFAULT_DB_ALIAS, models, connections
Expand All @@ -16,19 +17,37 @@
from common.result import Page


# 添加模型缓存
_model_cache = {}
def get_dynamics_model(attr: dict, table_name='dynamics'):
"""
获取一个动态的django模型
:param attr: 模型字段
:param table_name: 表名
:return: django 模型
"""
# 创建缓存键,基于属性和表名
cache_key = hashlib.md5(f"{table_name}_{str(sorted(attr.items()))}".encode()).hexdigest()
# print(f'cache_key: {cache_key}')

# 如果模型已存在,直接返回缓存的模型
if cache_key in _model_cache:
return _model_cache[cache_key]

attributes = {
"__module__": "knowledge.models",
"Meta": type("Meta", (), {'db_table': table_name}),
**attr
}
return type('Dynamics', (models.Model,), attributes)

# 使用唯一的类名避免冲突
class_name = f'Dynamics_{cache_key[:8]}'
model_class = type(class_name, (models.Model,), attributes)

# 缓存模型
_model_cache[cache_key] = model_class

return model_class


def generate_sql_by_query_dict(queryset_dict: Dict[str, QuerySet], select_string: str,
Comment thread
liuruibin marked this conversation as resolved.
Expand Down
Loading