Kim*_*cks 3 python django rest django-rest-framework
我正在使用 django rest 框架 3.6
我使用的前端库是x-editable,它需要了解该字段的数据类型。
我目前正在使用 Django Rest Framework 和 Serializers 获取我的数据。我在谷歌上搜索了 Serializer Field,但我很难理解它是否符合我的要求。我也不知道如何测试它是否合适。
基本上,我有一个端点试图获取 SomeModel 的单个实例及其 5 个相关模型。 /api/v1.0/shape/2508
这工作正常,我得到一个像这样的数据结构:
{
"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,
}
}
}
Run Code Online (Sandbox Code Playgroud)
django restframework 有没有办法传入一些关于相关模型字段的元数据?喜欢数据类型?
我正在寻找的最低限度是能够取回相关模型中字段的数据类型。
我希望能够检测数字、普通字符字段、文本字段
Django Rest Framework 有元数据类。但是您可以使用另一个名为drf-schema-adapte r 的库来扩充它。
'DEFAULT_METADATA_CLASS': 'drf_auto_endpoint.metadata.AutoMetadata',到您的REST_FRAMEWORK设置中DRF_AUTO_METADATA_ADAPTER = 'drf_auto_endpoint.adapters.ReactJsonSchemaAdapter'同一个文件中添加这个全新的设置它应该是这样的:
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'
Run Code Online (Sandbox Code Playgroud)
选择 ReactJsonSchemaAdapter 纯粹是个人喜好。您也可以坚持使用 DRF 本身的 SimpleMetadata。
使用类似 postman 的东西在同一个 url 上进行测试,但使用 OPTIONS 作为方法
你应该得到这样的东西:
{
"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
},
Run Code Online (Sandbox Code Playgroud)
如果我了解更多信息,将为此添加更多详细信息
| 归档时间: |
|
| 查看次数: |
952 次 |
| 最近记录: |