aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/fields.py
diff options
context:
space:
mode:
Diffstat (limited to 'rest_framework/fields.py')
-rw-r--r--rest_framework/fields.py31
1 files changed, 29 insertions, 2 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index d772c400..cb5f9a40 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -23,7 +23,8 @@ from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict
from rest_framework import ISO_8601
-from rest_framework.compat import timezone, parse_date, parse_datetime, parse_time
+from rest_framework.compat import (timezone, parse_date, parse_datetime,
+ parse_time)
from rest_framework.compat import BytesIO
from rest_framework.compat import six
from rest_framework.compat import smart_text, force_text, is_non_str_iterable
@@ -61,7 +62,8 @@ def get_component(obj, attr_name):
def readable_datetime_formats(formats):
- format = ', '.join(formats).replace(ISO_8601, 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]')
+ format = ', '.join(formats).replace(ISO_8601,
+ 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z]')
return humanize_strptime(format)
@@ -70,6 +72,18 @@ def readable_date_formats(formats):
return humanize_strptime(format)
+def humanize_form_fields(form):
+ """Return a humanized description of all the fields in a form.
+
+ :param form: A Django form.
+ :return: A dictionary of {field_label: humanized description}
+
+ """
+ fields = SortedDict([(name, humanize_field(field))
+ for name, field in form.fields.iteritems()])
+ return fields
+
+
def readable_time_formats(formats):
format = ', '.join(formats).replace(ISO_8601, 'hh:mm[:ss[.uuuuuu]]')
return humanize_strptime(format)
@@ -193,6 +207,19 @@ class Field(object):
return {'type': self.type_name}
return {}
+ @property
+ def humanized(self):
+ humanized = {
+ 'type': self.type_name,
+ 'required': getattr(self, 'required', False),
+ }
+ optional_attrs = ['read_only', 'help_text', 'label',
+ 'min_length', 'max_length']
+ for attr in optional_attrs:
+ if getattr(self, attr, None) is not None:
+ humanized[attr] = getattr(self, attr)
+ return humanized
+
class WritableField(Field):
"""