Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:37:36+00:00 2026-06-10T11:37:36+00:00

I am in process of migration from db.Model to ndb.Model . The only issue

  • 0

I am in process of migration from db.Model to ndb.Model. The only issue that I have to solve before finish this migration is that there is no Model.is_saved method. I have used db.Model.is_saved in my application to determine if sharded counters must be updated on put/delete, to check for conflicted keys on creating entities etc.

The documentation says that ndb.Model has no equivalent for is_saved method. I can reimplement some use cases with get_or_insert instead of is_saved. But not all of them.

As a dirty hack I can set flag like _in_memory_instance for every instance I have created by calling constructor. But it does not solve my issue. I still have to update this flag at least after every put() call.

The question is: is there better way to determine if model is persistent in the datastore or not without extra datastore hit?

Edit 1: Forgot to mention: all the entities got keys so check for Model._has_complete_key() does not work for me.

Edit 2: After this discussion https://groups.google.com/d/topic/google-appengine/Tm8NDWIvc70/discussion it seems to be the only way to solve my issue is to use _post_get_hook/_post_put_hook. I wondering why such a trivial thing was not included in official API.

Edit 3: I ended up with next base class for all my models. Now I can leave my codebase (almost) untouched:

class BaseModel(ndb.Model):

    @classmethod
    def _post_get_hook(cls, key, future):
        self = future.get_result()
        if self:
            self._is_saved = bool(key)

    def _post_put_hook(self, future):
        self._is_saved = future.state == future.FINISHING

    def is_saved(self):
        if self._has_complete_key():
            return getattr(self, "_is_saved", False)
        return False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:37:37+00:00Added an answer on June 10, 2026 at 11:37 am

    To get the same kind of state in NDB you would need a combination of
    post-get-hook and post-put-hook to set a flag. Here’s a working
    example:

    class Employee(ndb.Model):
      <properties here>
    
      saved = False  # class variable provides default value
    
      @classmethod
      def _post_get_hook(cls, key, future):
        obj = future.get_result()
        if obj is not None:
          # test needed because post_get_hook is called even if get() fails!
          obj.saved = True
    
      def _post_put_hook(self, future):
        self.saved = True
    

    There’s no need to check for the status of the future — when either
    hook is called, the future always has a result. This is because the
    hook is actually a callback on the future. However there is a need to
    check if its result is None!

    PS: Inside a transaction, the hooks get called as soon as the put() call returns; success or failure of the transaction doesn’t enter affect them. See https://developers.google.com/appengine/docs/python/ndb/contextclass#Context_call_on_commit for a way to run a hook after a successful commit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In one of my process I have this SQL query that take 10-20% of
I am starting migration process from python to jython. Has anyone done this with
Sorry for the 'svn' style - we are in a process of migration from
I was just testing the process of migration of data from M/S to HRD,
Are there any tools to facilitate a migration from Sourcegear's Vault to Subversion ?
I am currently in the process of migration to rails 3 from rails 2
I have a bootstrap script that performs syncdb and migrate : import settings from
We are in a migration process from a Progress DB to use the Dataserver
I am in the process of doing a data migration from OpenCart into Drupal
I'm currently in the process of beginning a migration from fixtures to factories and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.