【问题标题】:Generate view description based on parent docstring根据父文档字符串生成视图描述
【发布时间】:2014-07-22 16:37:49
【问题描述】:

如何从父视图中基于 Django-REST-Framework 为我的 API 生成基于类的视图描述(对嵌套资源有用)?

我有一个像这样的父类:

class ParentView(GenericAPIView):
    """
    My parent endpoint documentation
    """
    pass

还有一个像这样的子类:

class ChildView(ParentView):
    """
    My child endpoint documentation
    """
    pass

浏览子视图的交互式 API 时,生成的描述是“我的子端点文档”,但我想要“我的父端点文档”。

【问题讨论】:

    标签: python django django-views django-rest-framework


    【解决方案1】:

    解决方案是在子视图中重载get_view_description 方法,例如:

    class ChildView(ParentView):
        def get_view_description(self, html=False):
            """
            Get the view description based on the parent class docstring
            """
            func = self.settings.VIEW_DESCRIPTION_FUNCTION
            return func(self.__class__.__base__, html)
    

    这样做,您应该能够让父文档字符串记录您的子视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 2013-03-19
      相关资源
      最近更新 更多