I followed what is outlined here. Here is my code:
from google.appengine.api import users
from google.appengine.ext import db
class Book(db.Model):
title = db.StringProperty()
class Author(db.Model):
name = db.StringProperty()
class BookAuthor(db.Model):
book = db.ReferenceProperty(Book, required=True, collection_name='books')
author = db.ReferenceProperty(Author, required=True, collection_name='authors')
b = Book(title="My Book")
a = Author(name="Author of My Book")
db.put([b, a])
ba = BookAuthor(book=b, author=a)
ba.put()
b.authors
a.books
and I get AttributeError: 'Book' object has no attribute 'authors'
ReferenceProperties add query-objects as attributes to the referenced class. So look carefully at your mappings:
In your code:
So,
b
would have abooks
attribute, notauthors
. And,a
would have aauthors
attribute, notbooks
.If you change the collection names, your code should run.
Also, if
BookAuthor
does not have additional properties, make sure you look at the list-of-keys method outlined in the article you referenced.