views:

53

answers:

1

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:

class Comment(db.Model):
    version = db.ReferenceProperty(version.Version, collection_name="comments")

The problem is that instances of Version are not getting a comments property as I would expect. According to the docs, 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.

I know that the ReferenceProperty is set correctly, because I can get the Comments with this query:

        comments = comment.Comment.all().filter('version = ', self).order('-added_on').fetch(500)

but this does not:

        comments = self.comments.order('-added_on').fetch(500)

it crashes because self has no property comments.

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?

from version.py:

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

from comment.py:

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)
+3  A: 

It looks like you are overwriting the automagical property called comments with your own property comments, in this line:

comments = self.comments.order('-added_on').fetch(500)

What happens if you change the collection_name argument in your Comment model to be "comments_set", then change the above line to:

comments = self.comments_set.order('-added_on').fetch(500)
Peter Recore
Well, I'll be darned. It was the goofy collection name. I really don't think that the fundamental problem is that I was overwriting the property with that assignment, since I wasn't assigning to self.comments, just comments, but still, changing the collection name did the trick. Thanks for pushing me towards the obvious answer, @Peter. I had completely stonewalled myself.
Adam Crossland