mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-31 18:40:07 +03:00
Fixed array and relation types to add its child correct type, instead of always string.
This commit is contained in:
parent
0e10d32fb1
commit
600d1ee004
|
@ -23,6 +23,29 @@ from .utils import is_list_view
|
||||||
header_regex = re.compile('^[a-zA-Z][0-9A-Za-z_]*:')
|
header_regex = re.compile('^[a-zA-Z][0-9A-Za-z_]*:')
|
||||||
|
|
||||||
|
|
||||||
|
def _get_field_type(field):
|
||||||
|
"""
|
||||||
|
Gets the field coreschema type based on field's original type.
|
||||||
|
"""
|
||||||
|
default_return = coreschema.String
|
||||||
|
if field is None:
|
||||||
|
return default_return
|
||||||
|
|
||||||
|
return {
|
||||||
|
models.CharField: coreschema.String,
|
||||||
|
models.TextField: coreschema.String,
|
||||||
|
models.IntegerField: coreschema.Integer,
|
||||||
|
models.AutoField: coreschema.Integer,
|
||||||
|
models.BigAutoField: coreschema.Integer,
|
||||||
|
models.BigIntegerField: coreschema.Integer,
|
||||||
|
models.PositiveIntegerField: coreschema.Integer,
|
||||||
|
models.PositiveSmallIntegerField: coreschema.Integer,
|
||||||
|
models.SmallIntegerField: coreschema.Integer,
|
||||||
|
models.BooleanField: coreschema.Boolean
|
||||||
|
}.get(field.__class__, default_return)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def field_to_schema(field):
|
def field_to_schema(field):
|
||||||
title = force_text(field.label) if field.label else ''
|
title = force_text(field.label) if field.label else ''
|
||||||
description = force_text(field.help_text) if field.help_text else ''
|
description = force_text(field.help_text) if field.help_text else ''
|
||||||
|
@ -50,8 +73,11 @@ def field_to_schema(field):
|
||||||
description=description
|
description=description
|
||||||
)
|
)
|
||||||
elif isinstance(field, serializers.ManyRelatedField):
|
elif isinstance(field, serializers.ManyRelatedField):
|
||||||
|
schema_cls = coreschema.String
|
||||||
|
if field.child_relation and field.child_relation.queryset and field.child_relation.queryset.model:
|
||||||
|
schema_cls = _get_field_type(field.child_relation.queryset.model._meta.pk)
|
||||||
return coreschema.Array(
|
return coreschema.Array(
|
||||||
items=coreschema.String(),
|
items=schema_cls,
|
||||||
title=title,
|
title=title,
|
||||||
description=description
|
description=description
|
||||||
)
|
)
|
||||||
|
@ -60,8 +86,7 @@ def field_to_schema(field):
|
||||||
model = getattr(field.queryset, 'model', None)
|
model = getattr(field.queryset, 'model', None)
|
||||||
if model is not None:
|
if model is not None:
|
||||||
model_field = model._meta.pk
|
model_field = model._meta.pk
|
||||||
if isinstance(model_field, models.AutoField):
|
schema_cls = _get_field_type(model_field)
|
||||||
schema_cls = coreschema.Integer
|
|
||||||
return schema_cls(title=title, description=description)
|
return schema_cls(title=title, description=description)
|
||||||
elif isinstance(field, serializers.RelatedField):
|
elif isinstance(field, serializers.RelatedField):
|
||||||
return coreschema.String(title=title, description=description)
|
return coreschema.String(title=title, description=description)
|
||||||
|
@ -95,8 +120,6 @@ def field_to_schema(field):
|
||||||
description=description,
|
description=description,
|
||||||
format='date-time'
|
format='date-time'
|
||||||
)
|
)
|
||||||
elif isinstance(field, serializers.JSONField):
|
|
||||||
return coreschema.Object(title=title, description=description)
|
|
||||||
|
|
||||||
if field.style.get('base_template') == 'textarea.html':
|
if field.style.get('base_template') == 'textarea.html':
|
||||||
return coreschema.String(
|
return coreschema.String(
|
||||||
|
@ -104,7 +127,6 @@ def field_to_schema(field):
|
||||||
description=description,
|
description=description,
|
||||||
format='textarea'
|
format='textarea'
|
||||||
)
|
)
|
||||||
|
|
||||||
return coreschema.String(title=title, description=description)
|
return coreschema.String(title=title, description=description)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user