django - 在将 UUID 字段附加到列表中时,它还存储了字段的类型
问题描述
我有一个模型,它有一个 uuid 字段
public_id = models.UUIDField(
unique=True, editable=False, default=uuid.uuid4)
我遇到了一个问题
在过滤时我得到一个像这样的查询集 <obj1,obj2, obj3...>
我正在遍历它并将 public_id 保存在这样的单个列表中
associated_elements = list()
for i in query_set:
associated_elements.append(i.public_id)
print(associated_elements)
我得到这样的输出
[UUID('a3d69af6-9678-4c1a-8adf-3b9f3600c3b5'), UUID('c0cc4cbd-9b79-4e04-bbc2-dcfb52a9e3de'), UUID('86cd21a5-0833-4b46-8dd8-bf730a3014ee'), UUID('70596f50-2e00-44e9-9795-fce13d33fdbb'), UUID('e60c88b3-1066-4ddd-bab6-7a33a84dd998')]
但我需要这种格式的输出
['7e254d14-488a-45ed-8712-fe1a339d6c28', '40949d78-8252-4087-b69e-2143915b2317']
有什么办法吗?
解决方案
你可以试试下面,
associated_elements = list()
for i in query_set:
associated_elements.append(str(i.public_id))
print(associated_elements)
参考:
推荐阅读
- javascript - 我收到“未捕获的类型错误:无法设置未定义的属性“值”,但我的代码仍然有效
- java - 离开会话时 OpenVidu Android 应用程序崩溃?
- azure-devops - 在 Devops 部署期间添加 Azure Web 应用服务自定义域
- database - 如何在 Microsoft Access 中创建学生成绩单?
- java - 如何检索作业中的步数?
- generics - 使用子类类型参数覆盖泛型函数
- ruby-on-rails - 如何链接到枚举中的项目 - 无法投射枚举器错误
- arrays - 从指针的指针更改指针的值,该指针指向结构数组作为结构中的对象
- python - 如何在不通过 IDE 进入代码的情况下运行 python 游戏
- c# - 如何在 C# 中获取 Windows UWF 可用空间