def post(self):
    update = self.request.get('update')

    if users.get_current_user():
        if update:
            personal = db.GqlQuery("SELECT * FROM Personal WHERE __key__ = :1", db.Key(update))

            personal.name = self.request.get('name')
            personal.gender = self.request.get('gender')
            personal.mobile_num = self.request.get('mobile_num')
            personal.birthdate = int(self.request.get('birthdate'))
            personal.birthplace = self.request.get('birthplace')
            personal.address = self.request.get('address')
            personal.geo_pos = self.request.get('geo_pos')
            personal.info = self.request.get('info')
            photo = images.resize(self.request.get('img'), 0, 80)
            personal.photo = db.Blob(photo)
            personal.put()
            self.redirect('/admin/personal')
        else:
            personal= Personal()

            personal.name = self.request.get('name')
            personal.gender = self.request.get('gender')
            personal.mobile_num = self.request.get('mobile_num')
            personal.birthdate = int(self.request.get('birthdate'))
            personal.birthplace = self.request.get('birthplace')
            personal.address = self.request.get('address')
            personal.geo_pos = self.request.get('geo_pos')
            personal.info = self.request.get('info')
            photo = images.resize(self.request.get('img'), 0, 80)
            personal.photo = db.Blob(photo)
            personal.put()
            self.redirect('/admin/personal')

    else:
        self.response.out.write('I\'m sorry, you don\'t have permission to add this LP Personal Data.')

如果“更新”是包含密钥数据存储键的查询字符串,那么这会更新现有记录吗?我尝试了这个,但是不断添加新的记录/实体。请给我一些正确更新记录/实体的建议。
更正?:
def post(self):
    update = self.request.get('update')

    if users.get_current_user():
        if update:
            personal = Personal.get(db.Key(update))

            personal.name = self.request.get('name')
            personal.gender = self.request.get('gender')
            personal.mobile_num = self.request.get('mobile_num')
            personal.birthdate = int(self.request.get('birthdate'))
            personal.birthplace = self.request.get('birthplace')
            personal.address = self.request.get('address')
            personal.geo_pos = self.request.get('geo_pos')
            personal.info = self.request.get('info')
            photo = images.resize(self.request.get('img'), 0, 80)
            personal.photo = db.Blob(photo)
            personal.put()
            self.redirect('/admin/personal')
        else:
            personal= Personal()

            personal.name = self.request.get('name')
            personal.gender = self.request.get('gender')
            personal.mobile_num = self.request.get('mobile_num')
            personal.birthdate = int(self.request.get('birthdate'))
            personal.birthplace = self.request.get('birthplace')
            personal.address = self.request.get('address')
            personal.geo_pos = self.request.get('geo_pos')
            personal.info = self.request.get('info')
            photo = images.resize(self.request.get('img'), 0, 80)
            personal.photo = db.Blob(photo)
            personal.put()
            self.redirect('/admin/personal')

    else:
        self.response.out.write('I\'m sorry, you don\'t have permission to add this LP Personal Data.')

最佳答案

当您知道密钥时,无需执行查询:只需对密钥调用db.get()即可直接检索它,这比执行查询快得多。
至于每次创建新记录的原因,似乎您没有正确地将“更新”传递到页面。尝试记录查询字符串参数以查看出了什么问题。

关于python - GAE数据存储区Put(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2498264/

10-10 16:33