python、gae、ndb重写一个类方法,但它需要应用于实例吗?

2024-06-17 13:43:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何重写需要查询子类的特定属性的继承类方法?你知道吗

我不知道该怎么办。这就是我得到的:

    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() #?

如何重写基类中的\u方法,以便它还将筛选出子类的\u属性为False的键?你知道吗


Tags: 方法childbasereturn属性defkeys子类
2条回答

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

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)

相关问题 更多 >