Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a reason that a ReferenceProperty might not generate a back-reference?
    primarykey
    data
    text
    <p>In my current project, I have two models, Version and Comment. There is a one-to-many relationship between the two; each Version can have many Comment and the Comment model has a ReferenceProperty to record which Version it belongs to:</p> <pre><code>class Comment(db.Model): version = db.ReferenceProperty(version.Version, collection_name="comments") </code></pre> <p>The problem is that instances of Version are not getting a <em>comments</em> property as I would expect. <a href="http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ReferenceProperty" rel="nofollow noreferrer">According to the docs</a>, I should get an automagical property on each Version that is a query that returns all Comment instances that have their version set to the Version instance in question. Doesn't seem to work for my code.</p> <p>I know that the ReferenceProperty is set correctly, because I can get the Comments with this query:</p> <pre><code> comments = comment.Comment.all().filter('version = ', self).order('-added_on').fetch(500) </code></pre> <p>but this does not:</p> <pre><code> comments = self.comments.order('-added_on').fetch(500) </code></pre> <p>it crashes because self has no property comments.</p> <p>The complete code for the two models is included below. Does anyone have any idea why the back-reference property is not given to my Verson instance?</p> <p>from version.py:</p> <pre><code>from google.appengine.ext import db import piece class Version(db.Model): parent_piece = db.ReferenceProperty(piece.Piece, collection_name="versions") note = db.TextProperty() content = db.TextProperty() published_on = db.DateProperty(auto_now_add=True) def add_comment(self, member, content): import comment new_comment = None try: new_comment = comment.Comment() new_comment.version = self new_comment.author = member new_comment.author_moniker = member.moniker new_comment.content = content new_comment.put() except: # TODO: handle datastore exceptions here pass return new_comment def get_comments(self): import comment comments = None try: comments = comment.Comment.all().filter('version = ', self).order('-added_on').fetch(500) except: pass </code></pre> <p>from comment.py:</p> <pre><code>import version import member from google.appengine.ext import db class Comment(db.Model): version = db.ReferenceProperty(version.Version, collection_name="comments") author = db.ReferenceProperty(member.Member) author_moniker = db.StringProperty() author_thumbnail_avatar_url = db.StringProperty() content = db.TextProperty() added_on = db.DateProperty(auto_now_add=True) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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