Note that there are some explanatory texts on larger screens.

plurals
  1. POOne-to-Many relationship in ndb
    text
    copied!<p>I am reading up on Google app engine and preparing a sample to understand it better.</p> <p>In a nutshell the user can record an entry for every day in the month, like a calendar. And the user can view the entries on monthly basis. So no more than 30 ish at a time.</p> <p>Initially I had used <code>db</code> and the one-to-many relationship was straight forward.</p> <p>But once I came across the <code>ndb</code>, I realized there are two ways of modelling a one-to-many relationship.</p> <p>1) The structured property seems to act like a repeated property on the User model. Does it mean if I retrieve one user, I would automatically retrieve all the records she has entered? (e.g. the entire year) This isn't very efficient though, is it? I guess the the advantage is that you get all related data in one read operation. </p> <pre class="lang-py prettyprint-override"><code> from google.appengine.ext import ndb class User(UserMixin, ndb.Model): email = ndb.StringProperty(required = True) password_hash = ndb.TextProperty(required = True) record = ndb.StructuredProperty(Record, repeated=True) class Record(ndb.Model): notes = ndb.TextProperty() </code></pre> <p>2) Alternatively I could use perhaps the more classic way:</p> <pre class="lang-py prettyprint-override"><code> class User(UserMixin, ndb.Model): email = ndb.StringProperty(required = True) password_hash = ndb.TextProperty(required = True) class Record(ndb.Model): user = ndb.KeyProperty(kind=User) notes = ndb.TextProperty() </code></pre> <p>Which way is the better way in my use case?</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