Note that there are some explanatory texts on larger screens.

plurals
  1. POQuerying for not None
    text
    copied!<p>I have a model with a reference property, eg:</p> <pre><code>class Data(db.Model): x = db.IntegerProperty() class Details(db.Model): data = db.ReferenceProperty(reference_class = Data) </code></pre> <p>The data reference can be None.</p> <p>I want to fetch all Details entities which have valid data, ie for which the reference property is not None.</p> <p>The following works:</p> <pre><code>Details.all().filter('data !=', None).fetch(1000) </code></pre> <p>However, according to the <a href="http://code.google.com/appengine/docs/python/datastore/queriesandindexes.html#Introducing_Queries" rel="nofollow noreferrer">documentation on queries</a>, a <code>!=</code> query will actually perform two queries, which seems unnecessary in this case. Is <code>!=</code> optimised to only perform one query when used with None?</p> <p>Alternatively, <a href="http://groups.google.com/group/google-appengine-python/browse_thread/thread/18f3a9a4c4b690c8" rel="nofollow noreferrer">this post</a> mentions that NULL always sorts before valid values. Therefore the following also appears to work:</p> <pre><code>Details.all().filter('data &gt;', None).fetch(1000) </code></pre> <p>Although this would only do one query, using <code>&gt;</code> instead of <code>!=</code> makes the intent of what it is doing less obvious.</p> <p>As a third option, I could add an extra field to the model:</p> <pre><code>class Details(db.Model): data = db.ReferenceProperty(reference_class = Data) has_data = db.BooleanProperty() </code></pre> <p>As long as I keep has_data synchronised with data, I could do:</p> <pre><code>Details.all().filter('has_data =', True).fetch(1000) </code></pre> <p>Which way would be best?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload