在Django Rest框架的Swagger中显示最大值,最小值和默认值 [英] Show maximum, minimum and default in Swagger for Django Rest Framework

查看:735
本文介绍了在Django Rest框架的Swagger中显示最大值,最小值和默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于我的API的v2,我想正确地记录它。因为我使用Django Rest框架(3.1.1),并且在他们的文档中建议使用Swagger,我使用它。

I am developing a v2 for my API, and I would like to document it properly. Since I use Django Rest Framework (3.1.1), and it is recommended in their docs to use Swagger, I am using it.

在文档中,我看到其功能之一是显示字段默认值,最小值,最大值,只读和必需属性。但是,在我的示例中,我只看到描述,参数类型和数据类型,如图所示:

In the documentation, I see that one of its features is to show "Field default values, minimum, maximum, read-only and required attributes". However, in my example, I only see "Description", "Parameter Type" and "Data Type", as in the image:

我想看到更多的列,如最小值,最大值和默认值,从模型中获取的值(不必重写它们)在串行器中,这是我的序列化程序,以防它有助于:

I would like to see more columns, such as minimum, maximum, and default, with the values taken from the models (not having to rewrite them in the serializers. This is my serializer, in case it helps:

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id', 'email', 'first_name', 'last_name', 'password',)
        write_only_fields = ('password',)
        read_only_fields = ('id',)

    def create(self, validated_data):
    [...]

没有人似乎有同样的问题,所以也许(我希望不会)我错过了一些琐事,希望你能帮忙。谢谢!

Nobody seems to have the same problem so maybe (I hope not) I am missing something trivial. Hope you can help. Thanks!

推荐答案

class MySourceView(generics.RetrieveUpdateDestroyAPIView):
    """
    MySource documentation
    """
    serializer_class = MySourceSerializer
    queryset = MySource.objects.all()

    def get(self, request, *args, **kwargs):
        """
        GET Documentation
        ---
            parameters_strategy: replace
            parameters:
                - name: pk
                  description: "Liverpool FC is the best."
                  required: true
                  type: string
                  paramType: path
                  defaultValue: 5
        """
        return self.retrieve(request, *args, **kwargs)

你可以做到YAML docstring指定方法。 Swagger RESTful API文档规范中的详细信息4.3.3数据类型字段

You can do it as YAML docstring in specify method. More details in Swagger RESTful API Documentation Specification 4.3.3 Data Type Fields

这篇关于在Django Rest框架的Swagger中显示最大值,最小值和默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆