Q:

App engine ndb. Query model based on Reference Property

I am having some problems trying to query an object using the ndb in the AppEngine framework. Im very new to ndb and I think I am misunderstanding some important concepts.
Here is what I've got:
I have my model called Teacher:
from webapp2_extras.appengine.auth.models import User

class Teacher(ndb.Model):
    user = ndb.StructuredProperty(User)
    ranking = ndb.FloatProperty(required=True)

The user model, as you can see, comes from the webapp2 framework. Then I try to query the Teacher who's associated with some user. I do it this way:
def _findTeacher(self, user):
    t = Teacher.query(Teacher.user == user).fetch(1)
    if t:
        return t[0]
    else:
        return None

The user that comes as a parameter of this function is obtained as follows:
user_session = self.auth.get_user_by_session()
user = self.auth.store.user_model.get_by_auth_token(user_session['user_id'], user_session['token'])[0]

But the, I get the following error:
BadFilterError: invalid filter: Cannot query for non-empty repeated property auth_ids.

I would really like to know where is my mistake and it will also be very helpful an example of an app using ndb and the webapp2 auth system.
Thank you all

A:

From NDB Properties:

Although a StructuredProperty can be repeated and a StructuredProperty can contain another StructuredProperty, beware: if one structred property contains another, only one of them can be repeated.

The error is happening because Teacher.User is a StructuredProperty which contains Teacher.User.auth_ids, which is a repeated StructuredProperty.
Edit (solution):
I suggest doing this:
class Teacher(ndb.Model):
    user = ndb.KeyProperty(User)
    ranking = ndb.FloatProperty(required=True)

Store a reference to the User. For your example above this will work. However, it also means you cannot query for any properties of the user, such as Teacher.User.name == 'bob'.
And you query like this:
t = Teacher.query(Teacher.user == user.key).fetch(1)

I hope this helps.

