Django Rest Framework:如何从序列化程序类型生成模式?

时间:2018-11-20 15:13:20

标签: django django-rest-framework

我按照说明将django-rest-swagger添加到了django-rest-framework项目中。 GUI看起来不错,但是似乎由APIView的{​​{1}}字段自动生成的请求描述有一些问题。具体地

  • 嵌套的对象将被忽略,并显示为get_serializer
  • 所有{}类型都被列为string,而忽略了openapi string字段

例如,对于此序列化程序

format

GUI将请求显示为

class NestedSerializer(Serializer):
    firstname = StringField()

class RequestSerializer(Serializer):
    name = NestedSerializer()
    date = DateField()
    choice = ChoiceField(['aa', 'bb', 'cc'])

我是否缺少某些东西,或者该项目确实不支持更多特定类型?

1 个答案:

答案 0 :(得分:0)

我不确定您是否导入了正确的序列化程序字段。 { "name": "laravel/laravel", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "type": "project", "require": { "php": "^7.1.3", "Amranidev/scaffold-interface": "^1.7", "barryvdh/laravel-dompdf": "^0.8.4", "doctrine/dbal": "^2.9", "fideloper/proxy": "^4.0", "genealabs/laravel-caffeine": "^0.8.1", "h4cc/wkhtmltoimage-amd64": "0.12.x", "h4cc/wkhtmltoimage-i386": "0.12.x", "h4cc/wkhtmltopdf-amd64": "0.12.x", "h4cc/wkhtmltopdf-i386": "0.12.x", "laracasts/flash": "^3.0", "laravel/framework": "5.6.*", "laravel/tinker": "^1.0", "league/html-to-markdown": "^4.8", "maatwebsite/excel": "^3.1", "mtownsend/request-xml": "^1.0" }, "require-dev": { "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", "mockery/mockery": "^1.0", "nunomaduro/collision": "^2.0", "phpunit/phpunit": "^7.0" }, "autoload": { "classmap": [ "database/seeds", "database/factories" ], "psr-4": { "Ceamsa\\": "app/" }, "files": [ "app/Http/Helpers/ApiHelpers.php", "app/Http/Helpers/CertificateHelpers.php", "app/Http/Helpers/CommonHelpers.php", "app/Http/Helpers/CrudHelpers.php", "app/Http/Helpers/LanguageHelpers.php", "app/Http/Helpers/SapHelpers.php", "app/Http/Helpers/TemplateHelpers.php" ] }, "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }, "extra": { "laravel": { "dont-discover": [ ] } }, "scripts": { "post-root-package-install": [ "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], "post-create-project-cmd": [ "@php artisan key:generate" ], "post-autoload-dump": [ "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", "@php artisan package:discover" ] }, "config": { "preferred-install": "dist", "sort-packages": true, "optimize-autoloader": true }, "minimum-stability": "dev", "prefer-stable": true } 包中似乎没有包含StringField()

此外,rest_framework.serializers被标记为已弃用。作为django-rest-swagger回购django-rest-swagger的所有者,您应该使用drf-yasg,它同时支持以下两种情况:

  • 嵌套序列化器
  • django-rest-swagger