views:

216

answers:

1

I need to update a record in the datastore, but instead of updated record I get always a new record.

My model:

class PageModel(db.Model):

    title = db.StringProperty()
    content = db.TextProperty()
    reference = db.SelfReferenceProperty()

    user = db.UserProperty(auto_current_user = True)    
    created = db.DateTimeProperty(auto_now_add = True)
    modified = db.DateTimeProperty(auto_now = True)

    type = db.StringProperty()
    template = db.StringProperty()
    position = db.IntegerProperty()
    hidden = db.BooleanProperty()
    historical = db.BooleanProperty()

My handler:

class EditHandler(webapp.RequestHandler):

   def post(self):
        if self.request.path[6:] == '':
            page_id = 8
        else:
            page_id = int(self.request.path[6:]) # path: /edit/35

        #id = self.request.get('id')
        CP = PageModel.get_by_id(int(page_id))
        key = CP.key()

        title = self.request.get('title')
        content = self.request.get('content')
        type = self.request.get('type')
        hidden = self.request.get('hidden')
        #position = self.request.get('type')

        reference = PageModel.get_by_id(int(self.request.get('reference')))

        template = self.request.get('template')
        if ".htm" not in template:
            template = "index.htm"


        #if title == '' or content == '':
          #doRender(self,'create_page.htm',{'error' : 'Please fill in all fields'} )
          #return

        #edited_page = PageModel(key=CP.key, title=title, content=content, type=type, reference=reference, template=template)
        edited_page = PageModel()

        edited_page.key = CP.key()
        edited_page.title = title
        edited_page.content = content
        edited_page.type = type
        edited_page.reference = reference
        edited_page.template = template

        edited_page.put()

There should be something wrong with edited_page.key = CP.key(), or what?!

+2  A: 

Why are you created a new PageModel everytime? instead edit the one you got by id i.e. CP ? e.g.

edited_page = CP     
edited_page.title = title
edited_page.content = content
edited_page.type = type
edited_page.reference = reference
edited_page.template = template

edited_page.put()
Anurag Uniyal