Skip to main content
 首页 » 编程设计

python之Django,Dicts/Queryset 列表中的 Python X 值

2024年10月24日34mengfanrong

我得到了我所有的代码,

codes_in_db = DistribuidorCodes.objects.all().values('code') 
 
#[{'code': u'pelicano815'}, {'code': u'perro10189'}...] 

我想做的是做一个新的列表

myList = list() 
code = generate_code() 
if code not in codes_in_db: 
    myList.append(code) 

这段代码不起作用,我错过了什么..我知道应该是一种获取代码值(value)的方法......但我遇到了困难,而且我找不到一个问题可以做到这一点。

请您参考如下方法:

我认为您需要一个仅包含代码值的列表。你可以试试这个:

DistribuidorCodes.objects.all().values_list('code', flat=True) 

那应该返回这个:

[u'pelicano815', u'perro10189', ...]