hasattr returns always True for app engine ndb entity
<p>I am applying <a href="http://stackoverflow.com/a/19869907/492258">this answer</a> to my project</p>

<p>This is my ndb entity where is_deleted added later.</p>

<pre><code>class FRoom(ndb.Model):  
    location = ndb.StringProperty(default="")
    is_deleted = ndb.BooleanProperty(default=False) #added later 
    #other fileds
</code></pre>

<p>when I do print my entities with logging.info, I have</p>

<pre><code>FRoom(key=Key('FRoom', 5606822106890240), is_deleted=False, location=u'denizli')
FRoom(key=Key('FRoom', 6169772060311552), is_deleted=False, location=u'aydin' )
FRoom(key=Key('FRoom', 6451247037022208), location=u'bursa')
</code></pre>

<p>When I do for do </p>

<pre><code>for froom in frooms:
    logging.info(hasattr(froom, 'is_deleted')) # gives always True
</code></pre>

<p>but when I do for example:</p>

<pre><code>logging.info(hasattr(froom, 'is_deletedXXX')) #gives me False
</code></pre>

<p>What am I doing wrong?</p>

<p>This is expected behaviour due to the <code>is_deleted</code> property having the <code>default</code> option set: the datastore will automatically return that default value if the property was not explicitly set.</p>

<p>From the <a href="https://cloud.google.com/appengine/docs/python/ndb/entity-property-reference#options" rel="nofollow">Property Options</a> table:</p>

<p><a href="https://i.stack.imgur.com/KBDES.png" rel="nofollow"><img src="https://i.stack.imgur.com/KBDES.png" alt="enter image description here"></a></p>

<p>So for properties for which you set the <code>default</code> option in the model the check if the property exists is unnecessary - it always exists, so you can directly do:</p>

<pre><code>for froom in frooms:
    logging.info(froom.is_deleted)
    # or
    logging.info(getattr(froom, 'is_deleted'))
</code></pre>

<p>The <code>hasattr(froom, 'is_deletedXXX')</code> returns False because you don't have a <code>is_deletedXXX</code> property in <code>FRoom</code>'s model.</p>

python google-app-engine hasattr