首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在django-rest-swagger中将枚举呈现为dropdown

在django-rest-swagger中将枚举呈现为dropdown
EN

Stack Overflow用户
提问于 2018-11-14 17:48:56
回答 1查看 391关注 0票数 3

我正在尝试获取django-rest-swagger生成的swagger文档中显示的正确查询参数

代码语言:javascript
复制
from rest_framework.filters import BaseFilterBackend
from rest_framework.compat import coreapi, coreschema
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _

from enum import Enum

class MyChoices(Enum):
    ONE = 'ONE'
    TWO = 'TWO'

class KeyFilter(BaseFilterBackend):

    key_param = 'key'
    default_key = 'psc'
    key_title = _('Key')
    key_description = _('Specify the aggregation key.')

    def filter_queryset(self, request, queryset, view):
        key = request.query_params.pop(self.key_param, [self.default_key])[0]
        return ConsumptionAggregate(queryset).aggregate(key)

    def get_schema_fields(self, view):
        assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
        assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
        return [
            coreapi.Field(
                name=self.key_param,
                required=False,
                location='query',
                schema=coreschema.Enum(
                    MyChoices,
                    title=force_text(self.key_title),
                    description=force_text(self.key_description)
                )
            )
        ]

但这不会显示为下拉列表。

我们如何让选项呈现为dropdowns?

EN

回答 1

Stack Overflow用户

发布于 2020-12-07 14:35:31

尝试:

代码语言:javascript
复制
schema=coreschema.Enum(
  ('ONE', 'TWO'),
  title=force_text(self.key_title),
  description=force_text(self.key_description)
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53297220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档