views:

206

answers:

3
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.')
A: 

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.

Nick Johnson
Is db.get() above correct? how to log the query within the Local SDK?Thx Nick
Ivan Slaughter
Yes, the code you've used above should work, presuming 'update' is a string key - _not_ a key name. Use the logging module (eg, logging.warn) to log, and look at the console window to see logged messages.
Nick Johnson
BadKeyError: Invalid string key Raises, i get the 'update' string from a query string e.g. /personal?update=ahJsaXZlbGlob29kcHJvZHVjZXJyDwsSCFBlcnNvbmFsGM0wDA. How to assign this to be correct string key? Thx for your help Nick. I'm just verymuch newbie .... :-)
Ivan Slaughter
That does look like a string key (not a key name). You simply need to make sure it really is one, and that it's not being mangled/truncated/etc.
Nick Johnson
A: 

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))
Ivan Slaughter
A: 

Hello

I am Smita

I want display image from Google app engine datastore which is stored using blob property on pt(zpt) . I use python for programing .if you possible please tell me answer.

Thank you

Smita