Skip to content

Commit 101aff6

Browse files
Make autogenerated read only fields to be able to be nullable (#8536)
1 parent 9f07d9e commit 101aff6

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

rest_framework/utils/field_mapping.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ def get_field_kwargs(field_name, model_field):
9595
(hasattr(models, 'JSONField') and isinstance(model_field, models.JSONField)):
9696
kwargs['style'] = {'base_template': 'textarea.html'}
9797

98+
if model_field.null:
99+
kwargs['allow_null'] = True
100+
98101
if isinstance(model_field, models.AutoField) or not model_field.editable:
99102
# If this field is read-only, then return early.
100103
# Further keyword arguments are not valid.
@@ -104,9 +107,6 @@ def get_field_kwargs(field_name, model_field):
104107
if model_field.has_default() or model_field.blank or model_field.null:
105108
kwargs['required'] = False
106109

107-
if model_field.null:
108-
kwargs['allow_null'] = True
109-
110110
if model_field.blank and (isinstance(model_field, (models.CharField, models.TextField))):
111111
kwargs['allow_blank'] = True
112112

@@ -263,15 +263,15 @@ def get_relation_kwargs(field_name, relation_info):
263263
if not model_field.editable:
264264
kwargs['read_only'] = True
265265
kwargs.pop('queryset', None)
266+
if model_field.null:
267+
kwargs['allow_null'] = True
266268
if kwargs.get('read_only', False):
267269
# If this field is read-only, then return early.
268270
# No further keyword arguments are valid.
269271
return kwargs
270272

271273
if model_field.has_default() or model_field.blank or model_field.null:
272274
kwargs['required'] = False
273-
if model_field.null:
274-
kwargs['allow_null'] = True
275275
if model_field.validators:
276276
kwargs['validators'] = model_field.validators
277277
if getattr(model_field, 'unique', False):

0 commit comments

Comments
 (0)