gpt4 book ai didi

python - 每次调用 put 时是否更新 auto_now_add 属性?

转载 作者:行者123 更新时间:2023-11-28 22:04:03 24 4
gpt4 key购买 nike

我有数据库模型,其中一个属性有 auto_now_add=True。但看起来它不像我预期的那样工作 - 每次调用 put 时它都会更新(我使用我自己的 key_name,所以第二个/第三个/N put 的调用并没有真正创建新记录 - 只是更新它)。

代码如下:

class User(db.Model):
id = db.IntegerProperty()
added = db.DateTimeProperty(auto_now_add=True)
name = db.StringProperty()

...
# this handler can be called several times
def get(self):
user = User(key_name="id"+unique_user_id)
user.id = unique_user_id
user.name = current_name
user.put()

最佳答案

您每次都在重新创建实体,代码应如下所示:

#This will create the object only once
return User.get_or_insert(key_name="id%d" % unique_user_id,
id=unique_user_id,
name = current_name)

#This will create the object and update some properties
#You can't change the value of key_name
user = User.get_or_insert(key_name="id%d" % unique_user_id,
id=unique_user_id)
user.name = name
user.put()
return user

关于python - 每次调用 put 时是否更新 auto_now_add 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7783952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com