如果两列的数据等于特定字符串,则更新查询

2024-06-28 20:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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()

我已经通过嵌入链接的电子邮件发送了这个hashstring,当用户访问该链接时,该链接会被检查。我当前检查哈希(从链接中的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

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


Tags: the方法nameiddatastring链接as