GAE数据存储区()

时间:2010-03-23 07:42:52

标签: python google-app-engine google-cloud-datastore

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.')

如果'update'是包含密钥数据存储区密钥的查询字符串,它是否会更新现有记录。我尝试这个,但继续添加新的记录/实体。请给我一些关于正确更新记录/实体的意见。

校正? :

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.')

2 个答案:

答案 0 :(得分:0)

当您知道密钥时,无需进行查询:只需在密钥上调用db.get()即可直接检索它,这比执行查询要快得多。

至于您每次创建新记录的原因,看起来您没有正确地将“更新”传递到您的页面。尝试记录查询字符串参数以查看出现了什么问题。

答案 1 :(得分:0)

我终于自己回答了这个问题,感谢Nick Johnson对此的指导。 我无法将查询字符串url作为“字符串键”始终引发BadKeyError:无效的字符串键异常。

我尝试将这个'update'字符串作为隐藏字段放在html编辑表单上,这是有效的,因为'update'现在是一个有效的'字符串键'。

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

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