如果2列的数据等于特定字符串

时间:2018-04-06 21:45:40

标签: python django django-queryset

我的表包含用户查询数据。我通过执行以下操作生成散列字符串:

queries = Query.objects.values('id', 'name')

# creating a bytes string using the ID, NAME columns and a string "yes" (this string could be anything, I've considered yes as an example)
data = (str(query['id']) + str(query['name']) + "yes").encode()

link_hash = hashlib.pbkdf2_hmac('sha256', data, b"satisfaction", 100000)
link_hash_string = binascii.hexlify(link_hash).decode()

我已通过嵌入在链接中的电子邮件发送此哈希字符串,该链接在用户访问该链接时进行检查。我当前检查散列(从链接中的GET参数获得)是否与表中的某些数据匹配的方法是这样的:

queries = Query.objects.values('id', 'name')

# I've set replyHash as a string here as an example, it is generated by the code written above, but the hash will be got from the GET parameter in the link
replyHash = "269e1b3de97b10cd28126209860391938a829ef23b2f674f79c1436fd1ea38e4"

#Currently iterating through all the queries and checking each of the query
for query in queries:
    data = (str(query['id']) + str(query['name']) + "yes").encode()
    link_hash = hashlib.pbkdf2_hmac('sha256', data, b"satisfaction", 100000)
    link_hash_string = binascii.hexlify(link_hash).decode()
    if replyHash == link_hash_string:            
        print("It exists, valid hash")
        query['name'] = "BooBoo"
        query.save()
        break

这种方法的问题在于,如果我有一个包含数千行的大表,这种方法将花费大量时间。是否有使用注释或聚合的方法或其他能够在更短的时间内执行相同操作的方法?

0 个答案:

没有答案