开发者

GAE Datastore Put()

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

Should this will update the existing record if the 'update' is querystring containing key datastore key. I try this but keep adding new record/entity. Please give me some sugesstion to correctly updating the record/entity.

Correction? :

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


There's no need to do a query when you know the key: Simply call db.get() on the key to retrieve it directly, which is much faster than doing a query.

As to why you're creating a new record each time, it looks like you're not passing in 'update' to your page correctly. Try logging the query string parameters to see what's going wrong.


I finally answer this myself, Thanks for Nick Johnson guide on this. I cannot get the query string url as 'string key' that always raise BadKeyError: Invalid string key exception.

I try to put this 'update' string as hidden field on html edit form and this works since 'update' is a valid 'string key' now.

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜