首页 > 解决方案 > 在将 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']

有什么办法吗?

标签: djangolistdjango-views

解决方案


你可以试试下面,

associated_elements = list()
for i in query_set:
      associated_elements.append(str(i.public_id))
print(associated_elements)

参考:

https://docs.python.org/3/library/uuid.html


推荐阅读