使用Django Rest Framework时,如何将有关字段数据类型的信息传递给前端? [英] How to pass information about the field data type to the frontend when using Django Rest Framework?

查看:107
本文介绍了使用Django Rest Framework时,如何将有关字段数据类型的信息传递给前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django rest框架3.6

Am using django rest framework 3.6

我正在使用的前端库是 x可编辑,它需要

The frontend library I am using is x-editable, it requires knowledge of the datatype of the field.

我目前正在使用Django Rest Framework和Serializers获取数据。我已经在Google搜索了Serializer Field,但是我很难理解它是否符合我的要求。同样我也不知道如何去测试它是否适合。

I am currently fetching my data using Django Rest Framework and Serializers. I have googled about Serializer Field but I have difficulty understanding if it fits my requirements. Also I have no idea how to go about testing if it fits.

基本上,我有一个端点试图获取SomeModel及其5个相关模型的单个实例。 /api/v1.0/shape/2508

Basically I have a endpoint that attempts to fetch a single instance of SomeModel and its 5 related models. /api/v1.0/shape/2508

这行得通,我得到的数据结构类似于

This works okay and I get back a data structure that's like this:

{
    "data": {
        "art_numbers": [],
        "collection": "",
        "extra_number": "",
        "some_related_model1": {
            "finished_capacity": null,
            "finished_weight": null,
            "finished_width": null,
            "id": 3
        },
        "has_associated_product_variant": false,
        "id": 2508,
        "another_related_model": {
            "bar_height": null,
            "bar_number": "",
            "id": 3,

        }
    }
}

django restframework是否也可以传递有关相关模型字段的某些元数据?像数据类型一样?

Is there a way for django restframework to also pass in some metadata about the related model fields? like data type?

我要寻找的最小值是能够获取相关模型中字段的数据类型。

The minimum I am looking for is to be able to get back the data type of the fields inside the related models.

我希望能够检测数字,普通字符字段,文本字段

I want to be able to detect numbers, normal charfield, textfield

推荐答案

Django Rest Framework有元数据
类。
但是您可以使用另一个名为 drf-schema-adapte

Django Rest Framework has metadata classes. But you can augment it with another library called drf-schema-adapter.


  1. pip安装drf-schema-adapter,应为0.9.43

  2. 转到settings.py并将此'DEFAULT_METADATA_CLASS':'drf_auto_endpoint.metadata.AutoMetadata'添加到您的 REST_FRAMEWORK 设置

  3. 在此新设置中添加 DRF_AUTO_METADATA_ADAPTER ='drf_auto_endpoint.adapters.ReactJsonSchemaAdapter'在同一文件中

  1. pip install drf-schema-adapter which should be 0.9.43
  2. go to settings.py and add this 'DEFAULT_METADATA_CLASS': 'drf_auto_endpoint.metadata.AutoMetadata', to your REST_FRAMEWORK settings
  3. add in this brand new setting DRF_AUTO_METADATA_ADAPTER = 'drf_auto_endpoint.adapters.ReactJsonSchemaAdapter' in the same file

它应该看起来像这样:

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ],
    'DEFAULT_METADATA_CLASS': 'drf_auto_endpoint.metadata.AutoMetadata',
    'TEST_REQUEST_DEFAULT_FORMAT': 'json',
    'PAGE_SIZE': 20,
    'SEARCH_PARAM': 'q'
}

DRF_AUTO_METADATA_ADAPTER = 'drf_auto_endpoint.adapters.ReactJsonSchemaAdapter'

选择Rea ctJsonSchemaAdapter纯粹是个人喜好。您还可以坚持使用DRF本身的SimpleMetadata。

Choosing ReactJsonSchemaAdapter is a purely personal preference. You can also stick with the SimpleMetadata from DRF itself.

使用邮递员等工具在相同的网址上进行测试,但使用OPTIONS作为方法

use something like postman to test on the same url but using OPTIONS as the method

您应该找回这样的东西:

You should get back something like this:

{
    "data": {
        "name": "Shape Detail",
        "description": "Retrieve a shape by its id.",
        "renders": [
            "application/json"
        ],
        "parses": [
            "application/json",
            "application/x-www-form-urlencoded",
            "multipart/form-data"
        ],
        "actions": {
            "PUT": {
                "id": {
                    "type": "integer",
                    "required": false,
                    "read_only": true,
                    "label": "ID"
                },
                "name": {
                    "type": "string",
                    "required": true,
                    "read_only": false,
                    "label": "Shape Name",
                    "max_length": 100
                },
                "name_en": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "Shape Name [en]",
                    "max_length": 100
                },
                "name_zh_hans": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "Shape Name [zh-hans]",
                    "max_length": 100
                },
                "serial_number": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "Shape Number",
                    "max_length": 100
                },
                "shape_variant_number": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "Shape Variant Number",
                    "max_length": 100
                },
                "collection": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "Collection",
                    "max_length": 255
                },
                "qr_code": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "QR Code",
                    "max_length": 255
                },
                "extra_number": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "Extra Number",
                    "max_length": 255
                },
                "art_numbers": {
                    "type": "field",
                    "required": false,
                    "read_only": false,
                    "label": "Art numbers"
                },
                "remark": {
                    "type": "string",
                    "required": false,
                    "read_only": false,
                    "label": "Remark",
                    "max_length": 400
                },
                "has_associated_product_variant": {
                    "type": "field",
                    "required": false,
                    "read_only": true,
                    "label": "Has associated product variant"
                },
                "shape_benchmark": {
                    "type": "nested object",
                    "required": false,
                    "read_only": false,
                    "label": "Shape benchmark",
                    "children": {
                        "id": {
                            "type": "integer",
                            "required": false,
                            "read_only": true,
                            "label": "ID"
                        },
                        "up_spin_speed": {
                            "type": "string",
                            "required": false,
                            "read_only": false,
                            "label": "Up Spin Speed",
                            "max_length": 15
                        },

如果我要了解更多信息,将会在此添加更多详细信息

Will add more details to this should i learn more

这篇关于使用Django Rest Framework时,如何将有关字段数据类型的信息传递给前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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