如何覆盖继承的类方法,需要查询子类的特定属性?
我不确定该怎么做。这就是我所拥有的:
class base_class:
@classmethod
def a_method(cls, something):
return ndb.Query(kind=cls.__name__).fetch(keys_only=True)
@classmethod
def calls_a_method(cls, size=1, soemthing):
entity_keys = cls.a_method(something)
class child_class(base_class):
a_property = ndb.BooleanProperty()
def another_method():
stuff = child_class.calls_a_method() #?
如何覆盖base_class中的a_method,这样它也会过滤出child_class的a_property = False的键?
答案 0 :(得分:0)
我认为如果你在方法中分解查询,可以在子类中构造一个自定义查询:
class base_class:
@classmethod
def a_method(cls, something):
return ndb.Query(kind=cls.__name__)
@classmethod
def calls_a_method(cls, size=1, something):
entity_keys = cls.a_method(something).fetch(keys_only=True)
class child_class(base_class):
a_property = ndb.BooleanProperty()
@classmethod
def another_method(cls):
q = cls.a_method(something).filter(cls.a_property == False)
entity_keys = q.fetch(keys_only=True)
答案 1 :(得分:0)
这样的事情怎么样?
class base_class(ndb.Model):
@classmethod
def a_method(cls, something):
return cls.Query().fetch(keys_only=True)
@classmethod
def calls_a_method(cls, something):
entity_keys = cls.a_method(something)
class child_class(base_class):
a_property = ndb.BooleanProperty()
@classmethod
def another_method(cls, value):
return cls.calls_a_method(value)
@classmethod
def a_method(cls, something):
return cls.query(cls.a_property==something).fetch(keys_only=True)