简体   繁体   中英

Is there a reason that a ReferenceProperty might not generate a back-reference?

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM