문제

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

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top