Given an instance of a serializer field, return a dictionary of metadata about it.
(self, field)
| 112 | } |
| 113 | |
| 114 | def get_field_info(self, field): |
| 115 | """ |
| 116 | Given an instance of a serializer field, return a dictionary |
| 117 | of metadata about it. |
| 118 | """ |
| 119 | field_info = { |
| 120 | "type": self.label_lookup[field], |
| 121 | "required": getattr(field, "required", False), |
| 122 | } |
| 123 | |
| 124 | attrs = [ |
| 125 | 'read_only', 'label', 'help_text', |
| 126 | 'min_length', 'max_length', |
| 127 | 'min_value', 'max_value', |
| 128 | 'max_digits', 'decimal_places' |
| 129 | ] |
| 130 | |
| 131 | for attr in attrs: |
| 132 | value = getattr(field, attr, None) |
| 133 | if value is not None and value != '': |
| 134 | field_info[attr] = force_str(value, strings_only=True) |
| 135 | |
| 136 | if getattr(field, 'child', None): |
| 137 | field_info['child'] = self.get_field_info(field.child) |
| 138 | elif getattr(field, 'fields', None): |
| 139 | field_info['children'] = self.get_serializer_info(field) |
| 140 | |
| 141 | if (not field_info.get('read_only') and |
| 142 | not isinstance(field, (serializers.RelatedField, serializers.ManyRelatedField)) and |
| 143 | hasattr(field, 'choices')): |
| 144 | field_info['choices'] = [ |
| 145 | { |
| 146 | 'value': choice_value, |
| 147 | 'display_name': force_str(choice_name, strings_only=True) |
| 148 | } |
| 149 | for choice_value, choice_name in field.choices.items() |
| 150 | ] |
| 151 | |
| 152 | return field_info |