Question

i've this model

class User(ndb.Model):
    username = ndb.StringProperty()
    password = ndb.StringProperty()
    token = ndb.StringProperty(default="x")
    follows = ndb.KeyProperty(kind="User", repeated=True)
    picture = ndb.StringProperty()

right now i know, for each user, what are the people he follows. But, how can i know the people that follows me? example:

A.follows=[B,..]
C.follows=[B,..]
B.getWhoFollowsMe = [A,C]

basically, how can i create the getWhoFollowsMe function?

Was it helpful?

Solution 2

def getWhoFollowsMe(me_key):
  return User.query().filter(User.follows==me_key)

OTHER TIPS

You can use @Hernan's function, but you can also implement it as a property or ClassMethod for convenience.

class User(ndb.Model):
    username = ndb.StringProperty()
    password = ndb.StringProperty()
    token = ndb.StringProperty(default="x")
    follows = ndb.KeyProperty(kind="User", repeated=True)
    picture = ndb.StringProperty()

    @property
    def followers(self):
        return User.query().filter(self.follows == self.key).fetch(1000)

Thus later access it like

user.followers

But yeah maybe my answer is excessive and I think @Hernan's should be the correct accepted one

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top