def getWhoFollowsMe(me_key):
return User.query().filter(User.follows==me_key)
NDB Query: reverse relationships of ndb.KeyProperty
-
21-07-2023 - |
题
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?
解决方案 2
其他提示
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
不隶属于 StackOverflow