mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Stopgap fix for model fields rendering in browseable API
This commit is contained in:
parent
c30e0795be
commit
1a05942166
|
@ -238,15 +238,16 @@ class DocumentingHTMLRenderer(BaseRenderer):
|
||||||
return self.get_generic_content_form(view)
|
return self.get_generic_content_form(view)
|
||||||
|
|
||||||
# We need to map our Fields to Django's Fields.
|
# We need to map our Fields to Django's Fields.
|
||||||
field_mapping = dict([
|
# TODO: Remove this and just render serializer fields directly
|
||||||
[serializers.FloatField.__name__, forms.FloatField],
|
field_mapping = {
|
||||||
[serializers.IntegerField.__name__, forms.IntegerField],
|
serializers.FloatField: forms.FloatField,
|
||||||
[serializers.DateTimeField.__name__, forms.DateTimeField],
|
serializers.IntegerField: forms.IntegerField,
|
||||||
[serializers.DateField.__name__, forms.DateField],
|
serializers.DateTimeField: forms.DateTimeField,
|
||||||
[serializers.EmailField.__name__, forms.EmailField],
|
serializers.DateField: forms.DateField,
|
||||||
[serializers.CharField.__name__, forms.CharField],
|
serializers.EmailField: forms.EmailField,
|
||||||
[serializers.BooleanField.__name__, forms.BooleanField]
|
serializers.CharField: forms.CharField,
|
||||||
])
|
serializers.BooleanField: forms.BooleanField
|
||||||
|
}
|
||||||
|
|
||||||
# Creating an on the fly form see: http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
|
# Creating an on the fly form see: http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
|
||||||
fields = {}
|
fields = {}
|
||||||
|
@ -255,10 +256,13 @@ class DocumentingHTMLRenderer(BaseRenderer):
|
||||||
obj = view.object
|
obj = view.object
|
||||||
|
|
||||||
serializer = view.get_serializer(instance=obj)
|
serializer = view.get_serializer(instance=obj)
|
||||||
for k, v in serializer.fields.items():
|
for k, v in serializer.get_fields(True).items():
|
||||||
if v.readonly:
|
if v.readonly:
|
||||||
continue
|
continue
|
||||||
fields[k] = field_mapping[v.__class__.__name__]()
|
try:
|
||||||
|
fields[k] = field_mapping[v.__class__]()
|
||||||
|
except KeyError:
|
||||||
|
fields[k] = forms.CharField
|
||||||
|
|
||||||
OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
|
OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
|
||||||
if obj and not view.request.method == 'DELETE': # Don't fill in the form when the object is deleted
|
if obj and not view.request.method == 'DELETE': # Don't fill in the form when the object is deleted
|
||||||
|
|
|
@ -350,16 +350,16 @@ class ModelSerializer(RelatedField, Serializer):
|
||||||
"""
|
"""
|
||||||
Creates a default instance of a basic non-relational field.
|
Creates a default instance of a basic non-relational field.
|
||||||
"""
|
"""
|
||||||
field_mapping = dict([
|
field_mapping = {
|
||||||
[models.FloatField, FloatField],
|
models.FloatField: FloatField,
|
||||||
[models.IntegerField, IntegerField],
|
models.IntegerField: IntegerField,
|
||||||
[models.DateTimeField, DateTimeField],
|
models.DateTimeField: DateTimeField,
|
||||||
[models.DateField, DateField],
|
models.DateField: DateField,
|
||||||
[models.EmailField, EmailField],
|
models.EmailField: EmailField,
|
||||||
[models.CharField, CharField],
|
models.CharField: CharField,
|
||||||
[models.CommaSeparatedIntegerField, CharField],
|
models.CommaSeparatedIntegerField: CharField,
|
||||||
[models.BooleanField, BooleanField]
|
models.BooleanField: BooleanField
|
||||||
])
|
}
|
||||||
try:
|
try:
|
||||||
return field_mapping[model_field.__class__]()
|
return field_mapping[model_field.__class__]()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user