父在python中调用抽象方法

时间:2017-04-28 09:37:58

标签: python django python-2.7 python-3.x

我是python的新手。我想在我的父类中定义一些抽象方法并使用它们。我写了这样的代码

class BaseListAPI(APIView):
    __metaclass__  = abc.ABCMeta

    def get(self, request):
        search_object, start, count = 
        tools.handlers.read_get_request_data(request.GET)
        order_by = request.GET.get('order_by')

        return get_successful_response(self.get_query_set.list(
            without_user_serializer=self.get_serializer,
            search_object=search_object,
            start=start,
            count=count,
            order_by=order_by
        ))

    @abc.abstractmethod
    def get_query_set(self):
        """Method that should do something."""

    @abc.abstractmethod
    def get_serializer(self):
        """this method gets the serializer"""


class TourListListAPI(BaseListAPI):
    def get_query_set(self):
        return models.Tour.objects

    def get_serializer(self):
        return without_user.TourSerializer

当我调用self.get_query_set()时,我希望子函数调用,但它永远不会被调用,而抽象的父方法被调用会导致错误!我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

我犯了一个愚蠢的错误!它没有工作,因为它在调用函数时没有括号,是父类!现在这个工作就像一个魅力。

class BaseListAPI(APIView):
    __metaclass__  = abc.ABCMeta

    def get(self, request):
        search_object, start, count = 
        tools.handlers.read_get_request_data(request.GET)
        order_by = request.GET.get('order_by')

        return get_successful_response(self.get_query_set().list(
            without_user_serializer=self.get_serializer(),
            search_object=search_object,
            start=start,
            count=count,
            order_by=order_by
        ))

    @abc.abstractmethod
    def get_query_set(self):
        """Method that should do something."""

    @abc.abstractmethod
    def get_serializer(self):
        """this method gets the serializer"""