繁体   English   中英

python,gae,ndb,重写类方法,但需要将其应用于实例吗?

[英]python, gae, ndb, override a class method, but it needs to apply to the instance?

如何重写继承的类方法,该方法需要查询子类的特定属性?

我不确定该怎么做。 这就是我得到的:

    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的键?

我认为,如果您通过方法分解查询,则可以在子类中构造一个自定义查询:

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)

这样的事情怎么样?

    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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM