MCPcopy Create free account
hub / github.com/encode/django-rest-framework / get_field_kwargs

Function get_field_kwargs

rest_framework/utils/field_mapping.py:88–247  ·  view source on GitHub ↗

Creates a default instance of a basic non-relational field.

(field_name, model_field)

Source from the content-addressed store, hash-verified

86
87
88def get_field_kwargs(field_name, model_field):
89 """
90 Creates a default instance of a basic non-relational field.
91 """
92 kwargs = {}
93 validator_kwarg = list(model_field.validators)
94
95 # The following will only be used by ModelField classes.
96 # Gets removed for everything else.
97 kwargs['model_field'] = model_field
98
99 if model_field.verbose_name and needs_label(model_field, field_name):
100 kwargs['label'] = capfirst(model_field.verbose_name)
101
102 if model_field.help_text:
103 kwargs['help_text'] = model_field.help_text
104
105 max_digits = getattr(model_field, 'max_digits', None)
106 if max_digits is not None:
107 kwargs['max_digits'] = max_digits
108
109 decimal_places = getattr(model_field, 'decimal_places', None)
110 if decimal_places is not None:
111 kwargs['decimal_places'] = decimal_places
112
113 if isinstance(model_field, models.SlugField):
114 kwargs['allow_unicode'] = model_field.allow_unicode
115
116 if isinstance(model_field, models.TextField) and not model_field.choices or \
117 (postgres_fields and isinstance(model_field, postgres_fields.JSONField)) or \
118 (hasattr(models, 'JSONField') and isinstance(model_field, models.JSONField)):
119 kwargs['style'] = {'base_template': 'textarea.html'}
120
121 if model_field.null:
122 kwargs['allow_null'] = True
123
124 if isinstance(model_field, models.AutoField) or not model_field.editable:
125 # If this field is read-only, then return early.
126 # Further keyword arguments are not valid.
127 kwargs['read_only'] = True
128 return kwargs
129
130 if model_field.has_default() or model_field.blank or model_field.null:
131 kwargs['required'] = False
132
133 if model_field.blank and (isinstance(model_field, (models.CharField, models.TextField))):
134 kwargs['allow_blank'] = True
135
136 if not model_field.blank and (postgres_fields and isinstance(model_field, postgres_fields.ArrayField)):
137 kwargs['allow_empty'] = False
138
139 if isinstance(model_field, models.FilePathField):
140 kwargs['path'] = model_field.path
141
142 if model_field.match is not None:
143 kwargs['match'] = model_field.match
144
145 if model_field.recursive is not False:

Callers 1

build_standard_fieldMethod · 0.90

Calls 2

needs_labelFunction · 0.85
get_unique_validatorsFunction · 0.85

Tested by

no test coverage detected